在android中显示图像时如何解决Out of Memory问题?

时间:2012-08-18 07:18:03

标签: android out-of-memory

我正在开展测验申请。在那里我在列表中显示问题和选项。我在顶部保留了一个下一个按钮,当点击下一个按钮时,我就是 在测试结束之前一次又一次地调用同一页面。 由于某些问题由图片组成,我在问题下方显示一个按钮,当点击该按钮时,图像将显示在新页面中。 现在我的问题是,当我点击按钮显示正在显示outofmemory例外的图片时,我的问题就会出现。

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)

错误显示在myimgview.setImageResource(imageResource);行。我没有得到什么问题。因为我,我在可绘制的文件夹中保存了多达400张图像 需要我项目中的所有图像。有人请帮我解决这个问题。我正在努力解决这个问题。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:4)

当某些图像可能太大而无法显示时,会出现OutOfMemory Error

要解决此问题,请执行以下操作:

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);

答案 1 :(得分:0)

使用较小的图像。而已。尝试在使用之前减小图像尺寸。

答案 2 :(得分:0)

在显示之前,根据屏幕尺寸将图像缩小到ImageView的尺寸,这样就不会在低分辨率屏幕上显示高分辨率图像。它会节省很多内存。

答案 3 :(得分:0)

这可以帮助您实际减少图像的大小

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;
}