android - Image gets rotate though the orientation is 0 using exif while selecting from gallery -
i selecting image gallery , showing on image view. image getting selected, on samsung phones has issue of rotating image solve checking if image rotated or not usif exif interface , if rotated change angle.
but not working images. images can see straight, images if straight getting rotate.
as did debug orientation image 0, goes in default case , applies normal bitmap rotated bitmap. still image see rotated. not getting why happening..
private void onselectfromgalleryresult(intent data) { bitmap bm=null; if (data != null) { try { bm = mediastore.images.media.getbitmap(getapplicationcontext().getcontentresolver(), data.getdata()); } catch (ioexception e) { e.printstacktrace(); } } bytearrayoutputstream bytes = new bytearrayoutputstream(); bm = bitmap.createscaledbitmap(bm,512,512, true); bm.compress(bitmap.compressformat.png,100, bytes); file destination = new file(environment.getexternalstoragedirectory(), system.currenttimemillis() + ".png"); fileoutputstream fo; try { destination.createnewfile(); fo = new fileoutputstream(destination); fo.write(bytes.tobytearray()); fo.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } loadimagefromfile(destination.getabsolutepath()); } public void loadimagefromfile(string imagefile){ try { exifinterface ei = new exifinterface(imagefile); int orientation = ei.getattributeint(exifinterface.tag_orientation, exifinterface.orientation_undefined); bitmap bitmap = bitmapfactory.decodefile(imagefile); bitmap rotatedbitmap = null; log.e("orientation",string.valueof(orientation)+" check"); switch (orientation) { case exifinterface.orientation_rotate_90: rotatedbitmap = rotateimage(bitmap, 90); log.e("orientation",string.valueof(orientation)+" check"); break; case exifinterface.orientation_rotate_180: rotatedbitmap = rotateimage(bitmap, 180); log.e("orientation",string.valueof(orientation)+" check"); break; case exifinterface.orientation_rotate_270: rotatedbitmap = rotateimage(bitmap, 270); log.e("orientation",string.valueof(orientation)+" check"); break; case exifinterface.orientation_normal: rotatedbitmap = bitmap; log.e("orientation",string.valueof(orientation)+" check"); break; default: rotatedbitmap = bitmap; log.e("orientation",string.valueof(orientation)+" check"); break; } if(rotatedbitmap != null) { profile_image.setimagebitmap(rotatedbitmap); selectedbitmap = rotatedbitmap; bytearrayoutputstream stream = new bytearrayoutputstream(); selectedbitmap.compress(bitmap.compressformat.png, 100, stream); //replace 100 desired quality percentage. byte[] bytearray = stream.tobytearray(); file tempfile = file.createtempfile("temp",null, getcachedir()); fileoutputstream fos = new fileoutputstream(tempfile); fos.write(bytearray); mprofileimage = tempfile; } } catch (ioexception ex) { // uiutils.showalert(getstring(r.string.error),newgroupacvitity.this); } } public static bitmap rotateimage(bitmap source, float angle) { matrix matrix = new matrix(); matrix.postrotate(angle); return bitmap.createbitmap(source, 0, 0, source.getwidth(), source.getheight(), matrix, true); }
edit:
-
@suppresswarnings("deprecation") - private void onselectfromgalleryresult(intent data) { - - uri uri = (uri)data.getdata(); - string[] filepathcolumn = { mediastore.images.media.data }; - cursor cursor = getcontentresolver().query(uri,filepathcolumn, null, null, null); - if(cursor != null) { - cursor.movetofirst(); - int columnindex = cursor.getcolumnindex(filepathcolumn[0]); - string picturepath = cursor.getstring(columnindex); - cursor.close(); - - loadimagefromfile(picturepath); - } - }
@greenapps - use selected file this? tried this, not working in xiaomi devices changed code. there other way other devices?
what's going wrong here? can please? thank you.
loadimagefromfile(destination.getabsolutepath());
the image try load bitmap compressed jpg , saved file. destination
file
object file.
bitmaps not contain exif information. , hence jpg file not contain exif too.
so useless use exifinterface
on it.
sid saw code before. , told same story. maybe told it.
Comments
Post a Comment