android - Converting string to uri to bitmap to display in an ImageView -


i have looked on solution problem , can't seem figure out. i'm sure 1 or 2 simple lines , can steer me in right direction.

in app, user can click button open gallery. once select image, display image in imageview within app. part working fine. originally, had return uri gallery , directly display this:

imageview1.setimageuri(myuri); 

well, running dreaded "out of memory" error if user reloads page several times in row i'm having clean code scale down image. have done implementing bitmap class turns image bitmap , scales down me. now, imageview display code looks this:

imageview1.setimagebitmap(bitmap1); 

that part working fine well. here problem:

i convert uri path string , save in sharedpreference. when user exits application , comes later, image set automatically displays. convert uri this:

... selectedimageuri = data.getdata(); string selectedimagepath; selectedimagepath = getpath(selectedimageuri); ... 

the old method retrieve sharedpreference string, convert uri, display working fine. (except out of memory error of course) looked this:

uri myuri = uri.parse(selectedimagepath); imageview1 = setimageuri(myuri); 

"selectedimagepath" string retrieved sharedpreference. again, worked fine throw error if reloaded many times.

the part not working when try implement new bitmap conversion can scale bitmap , not memory error. here code that:

uri myuri = uri.parse(selectedimagepath) bitmap bitmap = getthumbnail(myuri); imageview1.setimagebitmap(bitmap); 

this displays nothing. original image choosing displays image fine when return screen , tries parse string sharedpreference , convert bitmap, nothing ever displays. code "getthumbnail" method taken directly post --->

how bitmap uri?

it 3rd answer down.

anyone have ideas? sorry super long post i'd rather on explain problem not give enough info. sorry if answered somewhere else. i've been hunting through other questions hours , have not found solves problem.

thanks.

i figured out here did else having unique problem. after image chosen gallery , returns intent, got data intent via code:

selectedimageuri = data.getdata(); 

then got path via this:

selectedimagepath = getpath(selectedimageuri); 

which made call "getpath" method:

public string getpath(uri uri)   {      cursor cursor = getcontentresolver().query(uri, null, null, null, null);     cursor.movetofirst();     int idx = cursor.getcolumnindex(mediastore.images.imagecolumns.data);     return cursor.getstring(idx); }  

then saved "selectedimagepath" sharedpreference string.

later, retrieve string , convert showing image, first retrieved sharedpreference string , converted "selectedimagepath". then, set in imageview this:

targetimage = (imageview)findviewbyid(r.id.imageview1); targgetimage.setimagebitmap(decodesampledbitmapfromresource(selectedimagepath, 200, 200)); 

which made call following 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 = 2;  if (height > reqheight || width > reqwidth) {     if (width > height) {         insamplesize = math.round((float)height / (float)reqheight);     } else {         insamplesize = math.round((float)width / (float)reqwidth);     } } return insamplesize; 

}

public static bitmap decodesampledbitmapfromresource(string resid,         int reqwidth, int reqheight) {      // first decode injustdecodebounds=true check dimensions     final bitmapfactory.options options = new bitmapfactory.options();     options.injustdecodebounds = true;     bitmapfactory.decodefile(resid, options);      // calculate insamplesize     options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight);      // decode bitmap insamplesize set     options.injustdecodebounds = false;     return bitmapfactory.decodefile(resid, options); } 

it's heck of lot of code simple task works i'm happy , moving on. else needs accomplish same thing.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -