I am trying to create an app that lets users click a button and select an image.
I used the second answer from this question to prompt the user for an image: How to pick an image from gallery (SD Card) for my app?
But sometimes if I choose an image that appears to be oriented correctly in the gallery, when I display it it will have a wrong orientation (I read that this usually happens with pictures taken by camera because they save their rotation in their ExifInterface
)
So this code runs when the user chooses an image:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri si = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(si);
Bitmap image = Utils.decodeUri(this, si);
addPerson(selectedName, selectedDate, image);
imageStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
addPerson()
adds the person to the list and calls saveData()
which is here:
try {
FileOutputStream out = new FileOutputStream(new File(folder, person.getName() + ".png"));
person.getImage().compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (IOException e) {
e.printStackTrace();
};
And on onCreate()
I run this code to load the items:
Bitmap notRotated = BitmapFactory.decodeFile(file.getPath());
Matrix matrix = new Matrix();
ExifInterface exif = new ExifInterface(file.getPath());
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = Utils.exifToDegrees(rotation);
if(rotation != 0f)
matrix.preRotate(rotationInDegrees);
image = Bitmap.createBitmap(notRotated, 0, 0, notRotated.getWidth(), notRotated.getHeight(), matrix, true);
notRotated.recycle();
But when I run the above code it just ends up with black images and when I debug it says rotation
is always 0 although there are obviously images with different rotation.
How can I fix this problem?
EDIT:
Here's my code to downsample my bitmaps to prevent OutOfMemoryException
s:
public static Bitmap decodeUri(Context context, Uri selectedImage) throws FileNotFoundException {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(context.getContentResolver().openInputStream(selectedImage), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 140;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(selectedImage), null, o2);
}
Could me downsampling affect the rotation?