android - Choosing Images on a Samsung Galaxy Device rotates -
i'm working on android-project, in user can upload image profile image. worked fine until point (eventually samsung-update). since then, images on samsung galaxy devices (maybe on others), on htc 1 xl works fine, on lenovo yoga tablet , on sony (don't know which) also. on galaxy's s6 , s5 rotates image. idea is, image set in "normal" perspective user took it. means, should take square, of course, had upright. samsung-devices head 90 degree wrong anticlockwise. code works on other devices. has same problem? idea? here code
// after image taken public void onactivityresult(int requestcode, int resultcode, intent data) { try { // data returned if (resultcode != fragmentactivity.result_canceled) { if (requestcode == result_load_image && resultcode == getactivity().result_ok){ // set profile image gallery try { uri selectedimage = data.getdata(); photo = mediastore.images.media.getbitmap(getactivity().getcontentresolver(), selectedimage); photo = mainutils.rotatebitmap(photo, data, getactivity()); photo = mainutils.resizebitmapifneeded(photo, 800, 800); byte[] bytearray; bytearrayoutputstream stream = new bytearrayoutputstream(); photo.compress(bitmap.compressformat.jpeg, 90, stream); bytearray = stream.tobytearray(); // save image parse parsefile , connect parseuser (redacted) }
mainutils methods:
public static bitmap resizebitmapifneeded(bitmap image, int maxwidth, int maxheight) { if (maxheight > 0 && maxwidth > 0) { int wid = image.getwidth(); int hei = image.getheight(); mainutils.log(" resizebitmapifneeded, size " + wid + " x " + hei); if (wid > maxwidth || hei > maxheight) { int width = image.getwidth(); int height = image.getheight(); float ratiobitmap = (float) width / (float) height; float ratiomax = (float) maxwidth / (float) maxheight; int finalwidth = maxwidth; int finalheight = maxheight; if (ratiomax > 1) { finalwidth = (int) ((float) maxheight * ratiobitmap); } else { finalheight = (int) ((float) maxwidth / ratiobitmap); } image = bitmap.createscaledbitmap(image, finalwidth, finalheight, true); wid = image.getwidth(); hei = image.getheight(); mainutils.log(" resizebitmapifneeded, resized size " + wid + " x " + hei); return image; } else { return image; } } else { return image; } } public static bitmap rotatebitmap(bitmap bitmap, intent data, context context) { int orientation = 0; try { uri selectedimage = data.getdata(); exifinterface exif; exif = new exifinterface(mainutils.getrealpathfromuri(context, selectedimage)); orientation = exif.getattributeint(exifinterface.tag_orientation, exifinterface.orientation_undefined); mainutils.log("orientation " + orientation); } catch (ioexception excep) { mainutils.log("rotate " + excep.getmessage()); } matrix matrix = new matrix(); switch (orientation) { case exifinterface.orientation_normal: return bitmap; case exifinterface.orientation_flip_horizontal: matrix.setscale(-1, 1); break; case exifinterface.orientation_rotate_180: matrix.postrotate(180); break; case exifinterface.orientation_flip_vertical: matrix.postrotate(180); matrix.postscale(-1, 1); break; case exifinterface.orientation_transpose: matrix.postrotate(90); matrix.postscale(-1, 1); break; case exifinterface.orientation_rotate_90: matrix.postrotate(90); break; case exifinterface.orientation_transverse: matrix.postrotate(-90); matrix.postscale(-1, 1); break; case exifinterface.orientation_rotate_270: matrix.postrotate(-90); break; default: return bitmap; } try { bitmap bmrotated = bitmap.createbitmap(bitmap, 0, 0, bitmap.getwidth(), bitmap.getheight(), matrix, true); bitmap.recycle(); return bmrotated; } catch (outofmemoryerror e) { e.printstacktrace(); return null; } } public static bitmap rotatebitmapfromfile(string picturepath) { int orientation = 0; bitmap bitmap = bitmapfactory.decodefile(picturepath); try { exifinterface exif = new exifinterface(picturepath); orientation = exif.getattributeint(exifinterface.tag_orientation, exifinterface.orientation_undefined); mainutils.log("orientation " + orientation); } catch (ioexception excep) { mainutils.log("rotate " + excep.getmessage()); } matrix matrix = new matrix(); switch (orientation) { case exifinterface.orientation_normal: return bitmap; case exifinterface.orientation_flip_horizontal: matrix.setscale(-1, 1); break; case exifinterface.orientation_rotate_180: matrix.postrotate(180); break; case exifinterface.orientation_flip_vertical: matrix.postrotate(180); matrix.postscale(-1, 1); break; case exifinterface.orientation_transpose: matrix.postrotate(90); matrix.postscale(-1, 1); break; case exifinterface.orientation_rotate_90: matrix.postrotate(90); break; case exifinterface.orientation_transverse: matrix.postrotate(-90); matrix.postscale(-1, 1); break; case exifinterface.orientation_rotate_270: matrix.postrotate(-90); break; default: return bitmap; } try { bitmap bmrotated = bitmap.createbitmap(bitmap, 0, 0, bitmap.getwidth(), bitmap.getheight(), matrix, true); bitmap.recycle(); return bmrotated; } catch (outofmemoryerror e) { e.printstacktrace(); return null; } }
gallery intent
intent intent = new intent( intent.action_pick, mediastore.images.media.external_content_uri); intent.settype("image/*"); startactivityforresult( intent.createchooser(intent, "select file"), galleryrequest);
duplicate question, see android image selected gallery orientation 0 : exif tag
check answer of mkjparekh. have do: 1.) retrieve orientation of bitmap media store 2.) rotate bitmap according orientation if necessary
Comments
Post a Comment