在屏幕旋转后创建位图时出现OutOfMemoryError

问题描述 投票:2回答:1

我正在使用PhotoView在我的应用程序中显示图像,但当我旋转设备3或4时我得到OutOfMemory错误,有什么问题?我是否正确清洁了PhotoView

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Get intent, action and MIME type
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                handleSendText(intent); // Handle text being sent
            } else if (type.startsWith("image/")) {
                handleSendImage(intent); // Handle single image being sent
            }
        } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
            if (type.startsWith("image/")) {
                handleSendMultipleImages(intent); // Handle multiple images being sent
            }
        } else {
            // Handle other intents, such as being started from the home screen
        }


    }



    void handleSendText(Intent intent) {
        String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (sharedText != null) {
            Toast.makeText(this, sharedText, Toast.LENGTH_LONG).show();
        }
    }

    void handleSendImage(Intent intent) {
        Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
        if (imageUri != null) {
            Toast.makeText(this, imageUri.toString(), Toast.LENGTH_LONG).show();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);//OOM here
                PhotoView photoView = (PhotoView) findViewById(R.id.img);
                photoView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    void handleSendMultipleImages(Intent intent) {
        ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
        if (imageUris != null) {
        }



    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ((PhotoView)findViewById(R.id.img)).setImageBitmap(null);
        ((PhotoView)findViewById(R.id.img)).setImageDrawable(null);
        ((PhotoView)findViewById(R.id.img)).setImageResource(0);
    }
}
java android out-of-memory
1个回答
1
投票

有时,仅将ImageView的位图或drawable设置为null是不够的。您需要显式回收位图。

为此,将变量bitmap转换为字段(全局变量)并在destroy上回收它。

public class MainActivity extends AppCompatActivity {

    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...    

加载位图时,加载如下:

if(bitmap != null) bitmap.recycle();
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

最后在onDestroy内部,将位图回收为:

@Override
protected void onDestroy() {
    ((PhotoView)findViewById(R.id.img)).setImageBitmap(null);
    if(bitmap != null) bitmap.recycle();
    super.onDestroy();
}
© www.soinside.com 2019 - 2024. All rights reserved.