1

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).

12
  • You think you do not need to tell the values of int reqWidth, int reqHeight and return inSampleSize; ? Commented Feb 12, 2016 at 10:19
  • the out of memory error is happen. There is much more info available. You think you dont have to post that? Commented Feb 12, 2016 at 10:20
  • 1
    Why dont you answer my questions? For instance Have you ever checked if the resulting bitmaps have size 50,50 or 10,10? Or Have you ever checked if the resulting bitmaps have size reqWidth,reqHeight? Well then do now and report. Commented Feb 13, 2016 at 8:59
  • 1
    Thats what i said before: Don't think so.. And what did i ask more? Commented Feb 13, 2016 at 10:14
  • 1
    Yes i know. Thats why i asked you long time ago to check and tell the values. Now do not accept that irrelevant answer. Commented Feb 13, 2016 at 13:07

1 Answer 1

2

You can use Glide or Picasso library for loading bitmap to ImageView.

Glide.with(context).load(urlOrUri).into(imageViewToLoad);
Sign up to request clarification or add additional context in comments.

4 Comments

it works, but why it get null pointer exception when I try to get its drawable. baseBitmap = ((BitmapDrawable) Back.getDrawable()).getBitmap(); i set back image using Glide.
You came into the garage to ask what was wrong with your car but then you bought a new car. Is that an answer?
@greenapps what? garage ? yes this answer works. but I have some problem same as stackoverflow.com/questions/34252258/…
What @greenapps trying to tell you is you asked one thing and then chosed as answer something totally different. About your problem now- add: listener to the Glide and acces the bitmap in the listener onResourceReady callback

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.