Android内存不足错误,将随机图像加载到一个图像视图中

时间:2014-09-06 00:09:23

标签: android image bitmap out-of-memory drawable

我无法理解如何实现我想要的目标。我试图将随机图像加载到drawables文件夹中的图像视图中。我通过获取对所有drawable的引用并将id放入int数组来实现此目的。我随机选择零和最大数组长度之间的数字,并用该图像填充imageview。

当我过度旋转屏幕时会出现问题,而是当我猜错了太多随机图像已加载到内存中时,因为抛出了内存不足异常。

我试图回收使用后的情况,但如果我再次将该图像重新拉回来,我会尝试重复使用回收的位图错误。

问题活动

package com.example.hip_hoptrivia;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class QuestionsActivity extends ActionBarActivity {

    private JSONObject dataBase = null;
    private JSONArray triviaQuestions = null;
    private int currentQuestion = 0, currentDrawable;
    //setup random question images
    private int[] questionImages = new int[] {R.drawable.boswell_cheo_mural, R.drawable.buddha, R.drawable.dq,
    R.drawable.equipment, R.drawable.graffiti, R.drawable.huge_image, R.drawable.rappers, R.drawable.spray_paint,
    R.drawable.wza};

    private static final String FILE_NAME = "questiondata.txt";
    private static final String TAG_QUESTION = "Question";
    private static final String TAG_ANSWER = "Answer";
    private static final String TAG_INFO = "Info";
    private static final String TAG_TRIVIA = "Trivia";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questions);
        createData();
        createQuestion();
        buttonSetup();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

/*        ImageView imageView = (ImageView)findViewById(R.id.randomImage);
        Drawable drawable = imageView.getDrawable();
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            bitmap.recycle();
            bitmap = null;
        }*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.questions, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        if(MyUtilities.NavigateActionItems( this , item))
            return true;
        else
            return super.onOptionsItemSelected(item);

    }

    private void createData(){

        String result = MyUtilities.parseLocalFile(this , FILE_NAME);

        if( result != null ){
            try{
                dataBase = new JSONObject( result );
                triviaQuestions = dataBase.getJSONArray(TAG_TRIVIA);

            }catch( JSONException e ){
                e.printStackTrace();
            }

        }else{
            Log.e("Parsing", "coudln't extract");
        }


    }

    private void createQuestion(){
        try{
            JSONObject s = triviaQuestions.getJSONObject(currentQuestion);
            String question = s.getString(TAG_QUESTION);

            //Clear Input Text
            EditText userInputEditText = (EditText)findViewById(R.id.userInput);
            userInputEditText.setText("");

            //Place Question Text
            TextView questionTextView = (TextView)findViewById(R.id.currentQuestion);
            questionTextView.setText(question);

            //Create random image that goes with text
            ImageView randomImage = (ImageView)findViewById(R.id.randomImage);
            //Random number such that [Min + (int)(Math.random() * ((Max - Min) + 1))]
            currentDrawable = (int)(Math.random() * ((questionImages.length-1) + 1));
            randomImage.setImageResource(questionImages[currentDrawable]);


        }catch( JSONException e ){
            e.printStackTrace();
        }
    }

    private void checkAnswer(){
        try{
            JSONObject s = triviaQuestions.getJSONObject(currentQuestion);
            String answer = s.getString(TAG_ANSWER);
            EditText userInputEditText = (EditText)findViewById(R.id.userInput);
            String userAnswer = userInputEditText.getText().toString();


            //check if input is correct
            if( answer.equalsIgnoreCase(userAnswer)){
                ++currentQuestion;
                createQuestion();
            }else{
                Toast.makeText(getApplicationContext(), "Wrong", 
                           Toast.LENGTH_LONG).show();
            }

        }catch( JSONException e ){
            e.printStackTrace();
        }

    }

    private void buttonSetup(){
        Button submitButton = (Button) findViewById(R.id.submitButton);
        submitButton.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //On input check if correct answer
                checkAnswer();
            }
        });
    }
}

布局文件

<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="com.example.hip_hoptrivia.QuestionsActivity" >

    <EditText
        android:id="@+id/userInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="112dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/currentQuestion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/userInput"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="115dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <Button
        android:id="@+id/submitButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/userInput"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="Submit" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/randomImage"
        android:layout_above="@+id/currentQuestion"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

首先,由于内存不足错误在显示图像时可能是一个严重问题,因此您需要正确引用Displaying Bitmaps Effeciently

其次

I have tried to recycle the after usage, but if I pull that image back up again i get a trying to reuse recycled bitmap error.

你应该使用

((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();

在换到新图像之前。

现在,如果您尝试交换已经回收的图像,则无法使用此功能。因此你会得到

 Canvas: trying to use a recycled bitmap error

onDestroy()中,您可以尝试

@Override
public void onDestroy() {
super.onDestroy();
imageView.setImageDrawable(null);
}

imageView替换为您的ImageView名称。希望这有帮助。