BitmapFactory.decodeFile causes outOfMemoryError only when I choose the image from the phone gallery, not when I choose the image from taking the photo with the camera intent. I actually avoid using decodeFile when using the camera intent. Can I use that same approach when choosing from the gallery (getting data from the intent)? Here is my code for getting the image in both ways:
If image was chosen from the device' gallery:
if (requestCode == 1)
{
if (intent != null && resultcode == RESULT_OK)
{
ImageHelper ih = new ImageHelper();
mProfilePicPath = ih.getSelectedImageFilePathFromGallery(this, intent);
Bitmap portraitPhoto = ih.getChosenImageFromGallery(mProfilePicPath);
try{
ih.saveImage("Profile pics", portraitPhoto, this);
ImageHelper.getChosenImageFromGallery:
public Bitmap getChosenImageFromGallery(String imagePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, options);
options.inSampleSize = DressingRoomActivity.calculateInSampleSize(options, 500, 500);
options.inJustDecodeBounds = false;
Bitmap portraitPhoto = ImageHelper.convertToPortraitOrientation(options, imagePath);
return portraitPhoto;
}
If image was chosen by taking photo using camera:
else if ((requestCode == CAMERA_REQUEST)&& (resultcode == Activity.RESULT_OK)){
Bundle extras = intent.getExtras();
Uri selectedImage = intent.getData();
if (extras != null) {
ImageHelper ih = new ImageHelper();
mProfilePicPath = ih.getFilePathFromCameraPhoto(this, selectedImage);
Bitmap photo = (Bitmap) intent.getExtras().get("data");
try{
ih.saveImage("Profile pics", photo, this);
As you can see, to grab the image from the gallery and get the Bitmap from it, I have to create the Bitmap to be only 500 x 500 pixels with this code options.inSampleSize = DressingRoomActivity.calculateInSampleSize(options, 500, 500);
Otherwise it causes an outOfMemoryError on the line Bitmap bmp = BitmapFactory.decodeFile(path, options);
Which is in the ImageHelper.convertToPortraitOrientation(options, imagePath);
method. This means the user gets AS IS quality in their image from the camera, and 500px quality from their image in the gallery.
My questions: 1) Is there any way to not make my image quality 500px when choosing from gallery? Like avoid this line somehow: Bitmap bmp = BitmapFactory.decodeFile(path, options);
or some other way?
2) With the gallery I had to convert to portrait which is only a minor annoyance but why is that? It flips the photo sideways if I don't.