Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android内存不足错误,将随机图像加载到一个图像视图_Android_Image_Bitmap_Out Of Memory_Drawable - Fatal编程技术网

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

Android内存不足错误,将随机图像加载到一个图像视图,android,image,bitmap,out-of-memory,drawable,Android,Image,Bitmap,Out Of Memory,Drawable,我不太明白如何实现我想要的。我正在尝试从drawables文件夹将随机图像加载到图像视图中。我通过获取对所有可绘图项的引用并将id放入int数组来实现这一点。然后随机选择一个介于零和最大数组长度之间的数字,并用该图像填充imageview 当我旋转屏幕太多时,问题就会出现,而我猜是当太多随机图像加载到内存中时,因为引发了内存不足异常 我曾尝试在使用后回收该图像,但如果我再次将该图像拉回,则会出现“尝试重新使用回收的位图”错误 问题活动 package com.example.hip_hoptri

我不太明白如何实现我想要的。我正在尝试从drawables文件夹将随机图像加载到图像视图中。我通过获取对所有可绘图项的引用并将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>

首先,由于显示图像时内存不足错误可能是一个严重的问题,因此您需要正确参考

其次

我已尝试在使用后回收该图像,但如果我再次将该图像拉回来,则会出现“尝试重新使用回收的位图”错误。

你应该使用

((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();
在更改为新图像之前

现在,如果您试图交换已回收的图像,这将不起作用。因此你会得到

 Canvas: trying to use a recycled bitmap error
在您的
ondestory()
中,您可以尝试

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

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

似乎只是添加了imageView。setImageDrawable(null)解决了这个问题。我认为它可以防止操作系统在切换映像时维护对映像的引用,从而将其标记为gc。非常感谢你!我会认为,通过关闭对drawable的引用,它会比丢失对旧图像的引用更重要。但是,在计算中,似乎你必须明确,你对gc的理解是绝对正确的。如果此答案解决了您的问题,请通过单击答案左侧的“右标记”来接受答案。接受答案将有助于访问此问题的任何其他人。因此,如果答案解决了您的问题,请不要忘记接受答案。如果您真的喜欢答案,也可以通过单击“向上”进行投票象征。