Reputation: 31
In my app, I'm just trying to select multiple images from gallary and show in recyclerview before uploading into server. In recyclerview, the images appear with original orientation. However before uploading into server, it's always rotating all vertical images 90 degree converting vertical image (width * height = 720 * 1280) into horizontal (width * height = 1280 * 720) format. I don't want my vertical images to change orientation at all. I checked all my code, I can conclude below code somehow making the rotation.
for (int i = 0; i < current_count; i++) {
index = i;
try {
assert getActivity() != null;
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uris.get(i));
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Log.d("Logs", "--width, height-" + width + "-" + height + "--");
converetdBitmap = getResizedBitmap(bitmap, 1280);
uploadImagesToDatabase(i, converetdBitmap, current_count);
} catch (IOException e) {
}
}
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
Log.d("Logs", "--width, height-" + width + "-" + height + "--");
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
Log.d("logs", "-- width, height-" + width + "-" + height + "--");
return Bitmap.createScaledBitmap(image, width, height, true);
}
In Log.d("Logs"....) output -- getting 1280 * 720 always while my original pic is of size 720 * 1280. Hence no issues in my function code getResizedBitmap(). The culprit seems to be this command - bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uris.get(i));
Please help me on this. Thank you !
Upvotes: 0
Views: 42
Reputation: 648
I used to met a similar issue when I try to display a image from the Camera. The reason for me is that I forgot to change the orientation of the image. Here are two convinent methods I used to read the orientation from File and Uri:
fun File.orientation(): Int {
try {
val exif = ExifInterface(absolutePath)
return when (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0)) {
6 -> 90
3 -> 180
8 -> 270
else -> 0
}
} catch (e: IOException) {
e.printStackTrace()
}
return 0
}
fun Uri.orientation(context: Context): Int {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
val exif = ExifInterface(context.contentResolver.openInputStream(this))
return when (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0)) {
6 -> 90
3 -> 180
8 -> 270
else -> 0
}
} catch (e: IOException) {
e.printStackTrace()
}
}
return 0
}
And then, you need to use the Bitmap#rotate()
to rotate the Bitmap accordingly.
Also, beware to scale the Bitmap before load it to the memory, since if we load too much memory, it might lead to an OOM. The best practice is to measure image size at first, for example,
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
options.inSampleSize = 1
uri.decodeBitmap(context, options)
here we use the inJustDecodeBounds
to tell the system we only need the size of image instead of loading the entire image to memory.
Image load in Android is somewhat intrigate, you could visit my open source project for details if you are interest: https://github.com/Shouheng88/Compressor .
Best wishes, hope it helps!
Upvotes: 1