0

I have images stored in drawable folder where its size 1mb and resolution of 992x9000 my code :

ImageView jpgView = (ImageView) findViewById(R.id.surahShow);
Uri path = Uri.parse("android.resource://com.android.ta/" + R.drawable.myimage);
jpgView.setImageURI(path);

apps running and force close

Here is my LogCat:

10-28 10:55:07.321: E/AndroidRuntime(596): FATAL EXCEPTION: main
10-28 10:55:07.321: E/AndroidRuntime(596): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
10-28 10:55:07.321: E/AndroidRuntime(596):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-28 10:55:07.321: E/AndroidRuntime(596):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:450)
10-28 10:55:07.321: E/AndroidRuntime(596):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:326)
10-28 10:55:07.321: E/AndroidRuntime(596):  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:349)
10-28 10:55:07.321: E/AndroidRuntime(596):  at com.android.juzamma.SingleListSurah.decodeSampleBitmapFromResource(SingleListSurah.java:290)
10-28 10:55:07.321: E/AndroidRuntime(596):  at com.android.juzamma.SingleListSurah.onCreate(SingleListSurah.java:199)
10-28 10:55:07.321: E/AndroidRuntime(596):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-28 10:55:07.321: E/AndroidRuntime(596):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
10-28 10:55:07.321: E/AndroidRuntime(596):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
10-28 10:55:07.321: E/AndroidRuntime(596):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-28 10:55:07.321: E/AndroidRuntime(596):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
10-28 10:55:07.321: E/AndroidRuntime(596):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 10:55:07.321: E/AndroidRuntime(596):  at android.os.Looper.loop(Looper.java:123)
10-28 10:55:07.321: E/AndroidRuntime(596):  at android.app.ActivityThread.main(ActivityThread.java:3647)
10-28 10:55:07.321: E/AndroidRuntime(596):  at java.lang.reflect.Method.invokeNative(Native Method)
10-28 10:55:07.321: E/AndroidRuntime(596):  at java.lang.reflect.Method.invoke(Method.java:507)
10-28 10:55:07.321: E/AndroidRuntime(596):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-28 10:55:07.321: E/AndroidRuntime(596):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-28 10:55:07.321: E/AndroidRuntime(596):  at dalvik.system.NativeStart.main(Native Method)

then i tried to change my code:

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight){

        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 2;

        if (height/2 < reqHeight || width/2 < reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
            }
        return inSampleSize;
    }

    public static Bitmap decodeSampleBitmapFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
        }
jpgView.setImageBitmap(decodeSampleBitmapFromResource(getResources(), R.drawable.myimage, 100, 100));

but still force close

i get solution Strange out of memory issue while loading an image to a Bitmap object and here

but the thing, I have not been able to adjust the their code with the code that I use

i do it on android 2.3.1

Can someone help

Community
  • 1
  • 1
noel
  • 1
  • 3

2 Answers2

1

your answer is UNIVERSAL IMAGE LOADER....have fun :) :)

Jeffy Lazar
  • 1,903
  • 13
  • 20
  • Jlazar89: thanks for your help i tried to import your project in my eclipse but why not materialize? – noel Oct 28 '13 at 15:43
  • Niraj Adhikari: I am not familiar with the database and also the application is not using databases – noel Oct 28 '13 at 15:49
0

You can use SmartImageView Library found on http://loopj.com/android-smart-image-view/. Learn it and use it.

Dimitri
  • 1,924
  • 9
  • 43
  • 65