如何在Android中显示图片时解决“内存不足"问题? [英] How to solve the Out of Memory issue while displaying image in android?

查看:60
本文介绍了如何在Android中显示图片时解决“内存不足"问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究测验应用程序.这样,我在列表中显示问题和选项.我在顶部保留了一个next按钮,当单击next按钮时,我 一次又一次地调用同一页面,直到测试结束. 由于某些问题由图像组成,因此我在问题下方显示一个按钮,单击该按钮时,图像将显示在新页面中. 现在,我的问题有时是当我单击按钮以显示其显示outofmemory异常的图像时.

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)

该错误显示在myimgview.setImageResource(imageResource);行中.我没有得到什么问题.我在可绘制文件夹中最多保留400张图像,因为我 在我的项目中需要所有这些图像.任何人都可以帮助我解决这个问题.我正在为这个问题而苦苦挣扎.任何帮助将不胜感激.

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.

推荐答案

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

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

这篇关于如何在Android中显示图片时解决“内存不足"问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆