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

coder android
  • 83
  • 1
  • 7
  • You think you do not need to tell the values of int reqWidth, int reqHeight and return inSampleSize; ? – greenapps Feb 12 '16 at 10:19
  • `the out of memory error is happen.` There is much more info available. You think you dont have to post that? – greenapps Feb 12 '16 at 10:20
  • You have to use smaller values for reqHeight & reqWidth. Also its better to use google`s code for this: http://developer.android.com/intl/es/training/displaying-bitmaps/load-bitmap.html#load-bitmap – X3Btel Feb 12 '16 at 10:35
  • `return inSampleSize;` Wjhat is its value? – greenapps Feb 12 '16 at 11:48
  • Have you ever checked if the resulting bitmaps have size 50,50 or 10,10? Don't think so. – greenapps Feb 12 '16 at 11:49
  • Please reread what i said and asked. Its not that difficult. Then give a normal answer. – greenapps Feb 12 '16 at 19:45
  • 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. – greenapps Feb 13 '16 at 08:59
  • I checked the resulting bitmap from decodeSampledBitmapFromUri() method when i call it by (100,100). but it is larger than that 100. it is its real size!!! – coder android Feb 13 '16 at 10:12
  • 1
    Thats what i said before: `Don't think so.`. And what did i ask more? – greenapps Feb 13 '16 at 10:14
  • inSampleSize is 1 !!!! the problem is here. some thing wrong is in this code. I should check it. – coder android Feb 13 '16 at 11:06
  • 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. – greenapps Feb 13 '16 at 13:07
  • About why it is different value- http://developer.android.com/intl/es/reference/android/graphics/BitmapFactory.Options.html#inSampleSize And please also read the link i provided before that- There its explained inSampleSize have to be power of two – X3Btel Feb 15 '16 at 10:49

1 Answers1

2

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

Glide.with(context).load(urlOrUri).into(imageViewToLoad);
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • 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. – coder android Feb 12 '16 at 11:51
  • 1
    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 Feb 12 '16 at 11:54
  • @greenapps what? garage ? yes this answer works. but I have some problem same as http://stackoverflow.com/questions/34252258/getdrawable-gives-null-object-while-trying-to-get-bitmap-from-imageview – coder android Feb 12 '16 at 12:45
  • 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 – X3Btel Feb 12 '16 at 15:21