android - ImageView will not load via setImageURI when ran on Samsung Galaxy S4 Only -
i've had specific issue when running basic code on samsung galaxy s4 (model: gt-i9500).
i implementing image picker via camera or gallery, , not life of me figure out why imageview blank when calling -
imageview.setimageuri(uri);
it wasn't until ran exact same code in emulator (and nexus 5) found samsung s4 issue.
full sample project can found on github & ready run
code used taken so post:
in oncreate:
btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { alertdialog.builder builder = new alertdialog.builder(context); builder.settitle("choose image source"); builder.setitems(new charsequence[]{"gallery", "camera"}, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { switch (which) { case 0: //launching gallery intent = new intent(intent.action_pick, mediastore.images.media.external_content_uri); startactivityforresult(i, gallery); break; case 1: //specify camera intent intent getcameraimage = new intent("android.media.action.image_capture"); file camerafolder; //check see if there sd card mounted if (android.os.environment.getexternalstoragestate().equals (android.os.environment.media_mounted)) camerafolder = new file(android.os.environment.getexternalstoragedirectory(), imagefolder); else camerafolder = mainactivity.this.getcachedir(); if (!camerafolder.exists()) camerafolder.mkdirs(); //appending timestamp "picture_" simpledateformat dateformat = new simpledateformat("yyyymmdd't'hhmmss"); string timestamp = dateformat.format(new date()); string imagefilename = "picture_" + timestamp + ".jpg"; file photo = new file(environment.getexternalstoragedirectory(), imagefolder + imagefilename); getcameraimage.putextra(mediastore.extra_output, uri.fromfile(photo)); //setting global variable used in onactivityresult imageuri = uri.fromfile(photo); startactivityforresult(getcameraimage, camera); break; default: break; } } }); builder.show(); } }); onactivityresult:
protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (resultcode == result_ok) { switch (requestcode) { case gallery: uri selectedimage = data.getdata(); imageview.setimageuri(selectedimage); break; case camera: imageview.setimageuri(imageuri); break; } } } also occurs when using picasso
if (resultcode == result_ok) { switch (requestcode) { case gallery: uri selectedimage = data.getdata(); picasso.with(context) .load(selectedimage) .into(imageview); break; case camera: picasso.with(context) .load(imageuri) .into(imageview); break; } } also occurs when using bitmap factory
try { bitmap bitmap = bitmapfactory.decodestream(context.getcontentresolver().openinputstream(imageuri)); imageview.setimagebitmap(bitmap); } catch (filenotfoundexception e) { e.printstacktrace(); } 
the results when ran on samsung s4 running 4.2.2

the results when ran on genymotion 2.4.0 running android 4.4.4

anyone know why happens?
so problem turns out image bitmaps being large samsung s4 handle.
frustratingly no errors thrown - correct solution follows:
switch (requestcode) { case gallery: bitmap bitmap = createscaledbitmap(getimagepath(data, getapplicationcontext()), imageview.getwidth(), imageview.getheight()); imageview.setimagebitmap(bitmap); break; case camera: string path = imageuri.getpath(); bitmap bitmapcamera = createscaledbitmap(path, imageview.getwidth(), imageview.getheight()); imageview.setimagebitmap(bitmapcamera); break; } helper methods:
// function image path imagepicker public static string getimagepath(intent data, context context) { uri selectedimage = data.getdata(); string[] filepathcolumn = {mediastore.images.media.data}; cursor cursor = context.getcontentresolver().query(selectedimage, filepathcolumn, null, null, null); cursor.movetofirst(); int columnindex = cursor.getcolumnindex(filepathcolumn[0]); string picturepath = cursor.getstring(columnindex); cursor.close(); return picturepath; } public bitmap createscaledbitmap(string pathname, int width, int height) { final bitmapfactory.options opt = new bitmapfactory.options(); opt.injustdecodebounds = true; bitmapfactory.decodefile(pathname, opt); opt.insamplesize = calculatebmpsamplesize(opt, width, height); opt.injustdecodebounds = false; return bitmapfactory.decodefile(pathname, opt); } public int calculatebmpsamplesize(bitmapfactory.options opt, int width, int height) { final int outheight = opt.outheight; final int outwidth = opt.outwidth; int samplesize = 1; if (outheight > height || outwidth > width) { final int heightratio = math.round((float) outheight / (float) height); final int widthratio = math.round((float) outwidth / (float) width); samplesize = heightratio < widthratio ? heightratio : widthratio; } return samplesize; }
Comments
Post a Comment