while executing this code I'm getting out of memory exception ,
Back.setImageBitmap(decodeSampledBitmapFromUri(myUri, 50, 50));
I use the below code for controlling this error, and it works fine but for larger image this error is still exist.
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 Bitmap decodeSampledBitmapFromUri(Uri uri,int reqWidth, int reqHeight) throws FileNotFoundException {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
}
complete error here:
02-12 14:14:51.888: E/AndroidRuntime(6560): FATAL EXCEPTION: main
02-12 14:14:51.888: E/AndroidRuntime(6560): java.lang.OutOfMemoryError
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:530)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:603)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.cut.BlurIt.decodeSampledBitmapFromUri(BlurIt.java:938)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.cut.BlurIt.onCreate(BlurIt.java:223)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.Activity.performCreate(Activity.java:5133)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.ActivityThread.access$600(ActivityThread.java:150)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.os.Looper.loop(Looper.java:213)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.ActivityThread.main(ActivityThread.java:5225)
02-12 14:14:51.888: E/AndroidRuntime(6560): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 14:14:51.888: E/AndroidRuntime(6560): at java.lang.reflect.Method.invoke(Method.java:525)
02-12 14:14:51.888: E/AndroidRuntime(6560): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
02-12 14:14:51.888: E/AndroidRuntime(6560): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-12 14:14:51.888: E/AndroidRuntime(6560): at dalvik.system.NativeStart.main(Native Method)
line 938:
BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
line 223:
Back.setImageBitmap(decodeSampledBitmapFromUri(myUri, 10, 10));
update:
I use this
InputStream in = null;
in = getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(in, null, options);
instead of this line
BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
and it works and load image without out of memory problem but I don't know why its return bitmap is not exactly what I called. I called (100,100) returned (172,171) and real size is (5508, 5472).