3

I am working on a quiz application. In that I am displaying the question and options in a list. I have kept a next button at the top and when the next button is clicked, I am calling the same page again and again until the test is over. Since some questions consist of images I am displaying a button below the question and when the button is clicked, the image will be displayed in the new page. Now my problem is sometimes when I click on the button to display the image it is displaying outofmemory exception.

image.java

public class image extends Activity {

    ImageView myimgview;
    ImageView buttondismiss;

    public static String image_url;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image);

        Intent intent = getIntent();
        image_url = intent.getStringExtra(List.IMAGE_NAME);

        buttondismiss = (ImageView) findViewById(R.id.dismiss1);

        myimgview = (ImageView) findViewById(R.id.imageView100);

        int in = image_url.indexOf(".");
        String index = Integer.toString(in);
        String picturename = image_url.substring(0, in);

        int imageResource = getResources().getIdentifier(picturename,"drawable", "com.quiz.test");                                              // getPackageName());
        myimgview = (ImageView) findViewById(R.id.imageView100);
        myimgview.setImageResource(imageResource);

    }


}

Logcat:

08-18 11:11:01.063: E/AndroidRuntime(552): FATAL EXCEPTION: main
08-18 11:11:01.063: E/AndroidRuntime(552): java.lang.OutOfMemoryError
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.content.res.Resources.loadDrawable(Resources.java:1937)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.content.res.Resources.getDrawable(Resources.java:664)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.widget.ImageView.resolveUri(ImageView.java:542)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.widget.ImageView.setImageResource(ImageView.java:315)
08-18 11:11:01.063: E/AndroidRuntime(552):  at com.quiz.test.image.onCreate(image.java:48)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.app.Activity.performCreate(Activity.java:4465)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.os.Looper.loop(Looper.java:137)
08-18 11:11:01.063: E/AndroidRuntime(552):  at android.app.ActivityThread.main(ActivityThread.java:4340)
08-18 11:11:01.063: E/AndroidRuntime(552):  at java.lang.reflect.Method.invokeNative(Native Method)
08-18 11:11:01.063: E/AndroidRuntime(552):  at java.lang.reflect.Method.invoke(Method.java:511)
08-18 11:11:01.063: E/AndroidRuntime(552):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-18 11:11:01.063: E/AndroidRuntime(552):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-18 11:11:01.063: E/AndroidRuntime(552):  at dalvik.system.NativeStart.main(Native Method)

The error is showing at the line myimgview.setImageResource(imageResource);. I didnt get what is the the problem. I kept upto 400 images in my drawable folder because I need all those images in my project. Anybody please help me with this issue. I am struggling with this issue. Any help will be greatly appreciated.

midhunhk
  • 5,560
  • 7
  • 52
  • 83
Amrutha
  • 575
  • 4
  • 9
  • 29
  • tell me the type of imageResource and what it contains ??? – Hardikgiri Goswami Aug 18 '12 at 07:22
  • 1
    please see my answer: http://stackoverflow.com/questions/10737582/bitmap-size-exceeds-vm-budget-error-android/10738115#10738115. It may not be your exact situation where OOME is coming, bt it has all the ways to solve the OOME problem. Please don't forget to upvote my answer if it solves your problem.Thank you :) – Shrikant Ballal Aug 18 '12 at 07:30

4 Answers4

4

The OutOfMemory Error occurs when some of the images may be too large to display.

To fix this you do something like this:

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);
Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
0

Use smaller images. That's it. Try reducing image size before using it.

0

Scale down the images to the ImageView's dimensions based on the screen size before displaying them so that you don't display a high res image on a low res screen. It will save a lot of memory.

Mus
  • 1,860
  • 2
  • 16
  • 19
0

This may help you to reduce size of an Image pragmatically

public Bitmap decodeFile(File f) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);
        // The new size we want to scale to
        final int REQUIRED_SIZE = 70;

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;
        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}
Rajesh
  • 2,618
  • 19
  • 25