简体   繁体   English

Android选择第二张图像时出现内存不足错误

[英]Android out of memory error on choosing second image

My app works by stepping out of main activity, starts a second activity, displays an image chosen by user and analyzes the image. 我的应用程序通过退出主要活动,开始第二个活动,显示用户选择的图像并进行分析来工作。

After the analysis of the first image, I used the back button to go back to the main activity and proceed to the second activity again to choose a second image. 在分析了第一张图像之后,我使用后退按钮返回到主要活动,然后再次进行第二个活动以选择第二张图像。 But as soon as the user chooses the second image, android give me an out of memory error. 但是,一旦用户选择了第二张图片,android就会给我一个内存不足的错误。 I tried keeping track of available memory. 我尝试跟踪可用内存。 The strange thing is that right before the second image is chosen, there is even more memory available than before the first image is chosen. 奇怪的是,在选择第二张图像之前,比选择第一张图像之前有更多的可用内存。 How should I go about solving this? 我应该如何解决这个问题? Thanks! 谢谢!

PS the code runs out of memory at BitmapFactory.decodeFile(picturePath); PS代码在BitmapFactory.decodeFile(picturePath)处的内存不足。

Assuming you are using the Bitmap class, you should call the recycle() method when you are done with the bitmap instance. 假设您正在使用Bitmap类,则在完成位图实例后应调用recycle()方法。

@Override 
protected void onDestroy(){
    super.onDestroy();
    mBitmap.recycle();
}

If you are running on pre 3.0 hardware, the memory value you see will not include the memory used by Bitmaps, so that's a possible reason for the behavior you described. 如果您在3.0之前的硬件上运行,则看到的内存值将包括位图使用的内存,因此这可能是您描述的行为的原因。

As a rule of thumb, you should always check the dimension of an image that your app retrieve dynamically (either from user selection or from the net), and scale it to the size that makes sense for your app. 根据经验,应始终检查应用程序动态检索的图像的尺寸(从用户选择或从网络中检索),并将其缩放到适合您的应用程序的尺寸。 For example, for a Gallery app it should rescale a picture the phone takes to the dimension of the screen. 例如,对于Gallery应用程序,它应该将手机拍摄的图片重新缩放到屏幕尺寸。 Below is code sample for decoding a scaled Bitmap: 下面是用于解码缩放位图的代码示例:

private Bitmap decodeFile(File f, int width_tmp, int height_tmp, int maxSize) {
    try {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        InputStream in = new FileInputStream(f);
        BitmapFactory.decodeStream(in, null, o);
        try {
            in.close();
        } catch (IOException e1) {
        }
        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (maxSize > 0) {
            if (width_tmp / 2 < maxSize
                    || height_tmp / 2 < maxSize) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale++;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        in = new FileInputStream(f);
        Bitmap bm = BitmapFactory.decodeStream(in, null, o2);
        try {
            in.close();
        } catch (IOException e1) {
        }
        return bm;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM