Reputation: 51
I have a problem when I take a photo with the camera in some devices and versions of Android. For example, in my Xperia U v4.0.3 my code works good but in another xperia U v2.3.3 it doesn't work...
I read a lot of about this error but I couldn't fix it...
My code to take a photo and show it:
public void callCamera(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, SELECT_CAMERA);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_CAMERA) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
try {
imageSelected = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
// I tried to read the image created for the camera in the Sd
//But I've got the same error...
//imageSelected = Utils.readImageSD(this, selectedImage.getPath());
imageSelected = Utils.rotateImage(Utils.scaleBitmap(imageSelected, 160, 160));
ivImageLoad.setImageBitmap(imageSelected);
} catch (Exception e) {
Utils.showToast(getApplicationContext(), R.string.errorLoad);
Log.e("Camera", e.toString());
}
}
}
I hope somebody can help me... I don't know what can I do....
Thanks in advance.
Regards.
Upvotes: 3
Views: 1936
Reputation: 7478
It looks like you only want to display a 160x160 bitmap, but on this line you are reading in the whole bitmap and then later scaling it down.
imageSelected = android.provider.MediaStore.Images.Media(cr, selectedImage);
You can avoid ever loading the whole bitmap into memory by using inJustDecodeBounds, and then you can scale it using inSampleSize.
Here is some example code from the Android series on Loading Large Bitmaps Efficiently
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromUri(Uri uri,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
}
Then to get your 160x160 image you would just do:
ivImageLoad.setImageBitmap(decodeSampledBitmapFromUri(selectedImage, 100, 100));
Upvotes: 4
Reputation: 2223
Maybe the origin of the problem is the default Heap size. There are ways to increase it:
For Android 2.2 or lower versions:
dalvik.system.VMRuntime.getRuntime().setMinimumHeapSize(sizeInBytes);
For Android 2.3 or or higher versions:
android:largeHeap="true"
Check if the source of the problem is this, increasing the heap accordingly.
Upvotes: 0