使用 ImageView 重复打开 Activity 导致内存不足错误

Repeated Opening of Activity with ImageView causes out of memory error

我有一个基本应用程序,它有一个按钮,可以通过意图打开 activity 并传递 1 到 9 之间的随机数,用于显示图像。

图片是 png 文件,每个文件大小约为 280KB。

该应用程序的一般使用显示结果 activity,然后用户点击后退按钮并再次单击该按钮以启动 activity 以显示另一张随机图像。 这工作正常,但是,在大约 6 或 7 次之后,我收到与 ResultActivity:

中相应的 setBackgroundResource 行相关的内存不足错误
result_image.setBackgroundResource(R.drawable.result2);

我尝试了很多方法 "unbindDrawables" 强制垃圾回收、将 imageView 设置为 null 等,但似乎没有任何效果。

对于这样一个基本的应用程序,我肯定错过了一些简单的东西?

通过按下按钮调用意图,如图所示:

/** Called when the user clicks the Send button */
    public void displayResult(View view) {
        int randomNum = minimum + (int) (Math.random() * maximum);
        Log.d(TAG, "result" + randomNum);

        Intent intent = new Intent(this, ResultActivity.class);
        intent.putExtra(EXTRA_MESSAGE, randomNum);
        startActivity(intent);
    }

显示图片的Activity如下图

public class ResultActivity extends Activity {
    private ImageView result_image;
    protected static final String TAG = "ResultActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        // Get the message from the intent
        Intent intent = getIntent();
        int message = intent.getIntExtra(ThumbMain.EXTRA_MESSAGE, 1);
        result_image = null;

        result_image = (ImageView) findViewById(R.id.result_image);

        TextView result_title = (TextView) findViewById(R.id.result_title);
        TextView result_description = (TextView) findViewById(R.id.result_description);

        Log.d(TAG, "message=" + message);
        switch (message) {
        case 1:
            result_image.setBackgroundResource(R.drawable.result1);
            result_title.setText(R.string.result1_title);
            result_description.setText(R.string.result1_description);
            break;
        case 2:
            result_image.setBackgroundResource(R.drawable.result2);
            result_title.setText(R.string.result2_title);
            result_description.setText(R.string.result2_description);
            break;
        case 3:
            result_image.setBackgroundResource(R.drawable.result3);
            result_title.setText(R.string.result3_title);
            result_description.setText(R.string.result3_description);
            break;
        case 4:
            result_image.setBackgroundResource(R.drawable.result4);
            result_title.setText(R.string.result4_title);
            result_description.setText(R.string.result4_description);
            break;
        case 5:
            result_image.setBackgroundResource(R.drawable.result5);
            result_title.setText(R.string.result5_title);
            result_description.setText(R.string.result5_description);
            break;
        case 6:
            result_image.setBackgroundResource(R.drawable.result6);
            result_title.setText(R.string.result6_title);
            result_description.setText(R.string.result6_description);
            break;
        case 7:
            result_image.setBackgroundResource(R.drawable.result7);
            result_title.setText(R.string.result7_title);
            result_description.setText(R.string.result7_description);
            break;
        case 8:
            result_image.setBackgroundResource(R.drawable.result8);
            result_title.setText(R.string.result8_title);
            result_description.setText(R.string.result8_description);
            break;
        case 9:
            result_image.setBackgroundResource(R.drawable.result9);
            result_title.setText(R.string.result9_title);
            result_description.setText(R.string.result9_description);
            break;
        default:
            result_image.setBackgroundResource(R.drawable.result1);
            break;
        }

    }
}

activity的xml在这里:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="org.example.android.ResultActivity"
    android:id="@+id/top_layout" >

    <ImageView
        android:id="@+id/result_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/result_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/result_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

堆栈跟踪:

01-13 22:44:20.545: E/AndroidRuntime(29561): FATAL EXCEPTION: main
01-13 22:44:20.545: E/AndroidRuntime(29561): Process: org.example.android.exampletest, PID: 29561
01-13 22:44:20.545: E/AndroidRuntime(29561): java.lang.OutOfMemoryError
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.content.res.Resources.loadDrawable(Resources.java:3426)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.content.res.Resources.getDrawable(Resources.java:1898)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.view.View.setBackgroundResource(View.java:16235)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at org.example.android.ResultActivity.onCreate(ResultActivity.java:50)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.Activity.performCreate(Activity.java:5451)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.ActivityThread.access0(ActivityThread.java:172)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.os.Looper.loop(Looper.java:136)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.ActivityThread.main(ActivityThread.java:5586)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at java.lang.reflect.Method.invokeNative(Native Method)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at java.lang.reflect.Method.invoke(Method.java:515)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at dalvik.system.NativeStart.main(Native Method)

来自 samgak 的评论 - "Try putting your images in the drawable-nodpi folder instead of drawable" 解决了问题。

非常感谢。