Image rotate when select from gallery
Problem: You would faced this when you select image from gallery and then set it in image view.
i have faced this issue in api level above 22 (above lollipop).In marshmallow portrait image rotate to landscape but landscape image doesn't rotate, so here is my solution to fixed image rotation problem.
Post all this code inside onActivityResult() methodif (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { selectedImage = data.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null); assert cursor != null; cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String Image = cursor.getString(columnIndex); Bitmap original = BitmapFactory.decodeFile(stDriverImage); cursor.close(); File curFile = new File(stDriverImage); try { ExifInterface exif = new ExifInterface(curFile.getPath()); int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); int rotationInDegrees = exifToDegrees(rotation); Matrix matrix = new Matrix(); if (rotation != 0f) { matrix.preRotate(rotationInDegrees); } bmp = Bitmap.createBitmap(original, 0, 0, original.getWidth(), original.getHeight(), matrix, true); } catch (IOException e) { private int exifToDegrees(int exifOrientation) { if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; } return 0; }In Lollipop:put below code in manifest fileandroid:largeHeap="true" android:hardwareAccelerated="false"
No comments :
Post a Comment