1

I have converted an image(String url) using a bitmap but sometimes it produces an out of memory error. Here is my code:

        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE=150;
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale++;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
Uooo
  • 6,204
  • 8
  • 36
  • 63
Vishnu
  • 711
  • 2
  • 6
  • 11
  • Use less memory. You don't have enough to decode that bitmap. If you have other large allocations like big bitmaps in your app you need to recycle them. If its just a huge image, your device may not be able to display it. – Gabe Sechan Aug 13 '13 at 06:24
  • It's better to have a refference on created FileInputStream and then call `close()` for it when it's not needed anymore. – antonv Aug 13 '13 at 06:24
  • For creating bitmap-http://stackoverflow.com/questions/17990086/out-of-memory-while-creating-bitmaps-on-device/17990482#17990482 – T_V Aug 13 '13 at 06:26
  • and for recycling-http://stackoverflow.com/questions/17744828/bitmap-and-outofmemory-in-android/17745049#17745049 – T_V Aug 13 '13 at 06:28
  • I think the OOM issue related to somewhere above the code you posted here. Because of two reasons: 1. You have calculated the size and decode in sample size, it should work fine. 2. As I see in your question, it's a remote image using url, but in the code you're using FileInputStream. Please provide the code from where you handle the url so that we can see things more clearly. – Binh Tran Aug 13 '13 at 11:51

2 Answers2

0

First of all, reduce the size of an image. You can do it without drastically change the quality. And for an experiment just try to save your image as a bitmap. You will be shocked. If, for instance, your jpg is 3MB in size, it will be about 21MB after converting to bmp - huge number!

yobibyte
  • 193
  • 7
0
    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
            try {
                bmp = BitmapFactory
                        .decodeStream(
                                getContentResolver().openInputStream(
                                        imageFileUri), null, bmpFactoryOptions);
                bmpFactoryOptions.inSampleSize = quality;
                System.out.println("The Quality is" + quality);
                bmpFactoryOptions.inJustDecodeBounds = false;
                bmp = BitmapFactory
                        .decodeStream(
                                getContentResolver().openInputStream(
                                        imageFileUri), null, bmpFactoryOptions);
} catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            String[] orientationColumn = { ImageColumns.ORIENTATION };
            @SuppressWarnings("deprecation")
            Cursor cur = managedQuery(imageFileUri, orientationColumn, null,
                    null, null);
            int orientation = -1;
            if (cur != null && cur.moveToFirst()) {
                orientation = cur.getInt(cur
                        .getColumnIndex(orientationColumn[0]));
            }
            Matrix matrix = new Matrix();
            matrix.postRotate(orientation);
            bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
                    bmp.getHeight(), matrix, true);
            view.setImageBitmap(bmp);

int quality = 4;

find it more effective with big bitmaps.

Kosh
  • 6,140
  • 3
  • 36
  • 67
  • Heads up that System.out.println() wont work on android (thats what OP asked for) – Akshat Agarwal Dec 10 '13 at 20:25
  • 1
    @AkshatAgarwal it won't display in android but it will display inside the logcat !!!! – Kosh Dec 11 '13 at 05:15
  • 1
    well i'm glad you learnt something from me. and do know that android is base on java. system.out will still work, even system.exit(0). as system.out.print(); it display only at the output panel of IDE. – Kosh Dec 13 '13 at 10:35