android - Converting image to bitmap and reducing the size of it -


i have @ many different methods of creating reduced size bitmap of image, none of them work / need different.

this little difficult explain :-)

what need bitmap keeps ratio of picture, less size - eg 1mb or equivalent in pixel dimensions (as bitmap needs added putextra() intent).

problems i'm having far:

most of methods i've looked @ create scaled version of bitmap. so: image -> bitmap1 (unscaled) -> bitmap2 (scaled). if resolution of image high, not scaled down enough. think solution create bitmap of exact size resolution can reduced enough.

however, side effect of method images less required size resized (or resizing won't work?). there needs "if" check if image can converted bitmap without resizing.

i have no idea how go doing appreciated! :-)

this i'm using @ moment (it not want do):

// called when image picked gallery @override     public void onactivityresult(int requestcode, int resultcode,             intent imagereturnedintent) {         super.onactivityresult(requestcode, resultcode, imagereturnedintent);          switch (requestcode) {          case 0:             if (resultcode == activity.result_ok) {                 selectedimage = imagereturnedintent.getdata();                 viewimage = imagereturnedintent.getdata();                  try {                     decodeuri(selectedimage);                 } catch (filenotfoundexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }                  iv_preview.setimagebitmap(mimagebitmap);             }             break; // rest unnecessary 

this part scaling size:

private bitmap decodeuri(uri selectedimage) throws filenotfoundexception {            // decode image size         bitmapfactory.options o = new bitmapfactory.options();         o.injustdecodebounds = true; //         bitmapfactory.decodestream(getactivity().getcontentresolver()                 .openinputstream(selectedimage), null, o);          // new size want scale         final int required_size = 260; // kilobites? 306          // find correct scale value. should power of 2.         int width_tmp = o.outwidth, height_tmp = o.outheight;         int scale = 1;         while (true) {             if (width_tmp / 2 < required_size || height_tmp / 2 < required_size) {                 break;             }             width_tmp /= 2;             height_tmp /= 2;             scale *= 2;         }           // decode insamplesize         bitmapfactory.options o2 = new bitmapfactory.options();         o2.insamplesize = scale;         o2.inscaled = false; // better quality?         mimagebitmap = bitmapfactory.decodestream(getactivity()                 .getcontentresolver().openinputstream(selectedimage), null, o2);         return bitmapfactory.decodestream(getactivity().getcontentresolver()                 .openinputstream(selectedimage), null, o2);      } 

if needs explained more please say.

thank you

how call:

protected void onactivityresult(int requestcode, int resultcode, intent data) {     super.onactivityresult(requestcode, resultcode, data);          uri selectedimage = data.getdata();         string[] filepathcolumn = { mediastore.images.media.data };          cursor cursor = getcontentresolver().query(selectedimage,                 filepathcolumn, null, null, null);         cursor.movetofirst();          int columnindex = cursor.getcolumnindex(filepathcolumn[0]);         string picturepath = cursor.getstring(columnindex);         cursor.close();         pho1.setimagebitmap(decodesampledbitmapfromresource(picturepath,                 80, 60)); 

methods:

public static int calculateinsamplesize(bitmapfactory.options options,             int reqwidth, int reqheight) {         // raw height , width of image         final int height = options.outheight;         final int width = options.outwidth;         int insamplesize = 1;          if (height > reqheight || width > reqwidth) {              // calculate ratios of height , width requested height ,             // width             final int heightratio = math.round((float) height                     / (float) reqheight);             final int widthratio = math.round((float) width / (float) reqwidth);              // choose smallest ratio insamplesize value,             // guarantee             // final image both dimensions larger or equal             // requested height , width.             insamplesize = heightratio < widthratio ? heightratio : widthratio;         }          return insamplesize;     }      public static bitmap decodesampledbitmapfromresource(string path,             int reqwidth, int reqheight) {         log.d("path", path);         // first decode injustdecodebounds=true check dimensions         final bitmapfactory.options options = new bitmapfactory.options();         options.injustdecodebounds = true;         bitmapfactory.decodefile(path, options);          // calculate insamplesize         options.insamplesize = calculateinsamplesize(options, reqwidth,                 reqheight);          // decode bitmap insamplesize set         options.injustdecodebounds = false;         return bitmapfactory.decodefile(path, options);     } 

Comments

Popular posts from this blog

javascript - JS causing window size to be bigger than necessary - Dropdown bug -

php - Calling a template part from a post -

How to mention the localhost in android -