【问题标题】:How to avoid an out of memory error while using bitmaps in Android如何在 Android 中使用位图时避免内存不足错误
【发布时间】:2013-10-03 05:50:50
【问题描述】:

我正在使用位图。代码运行时显示内存不足错误。如何避免错误。我的代码如下。提前致谢。

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500); 
img_cook[index].setImageBitmap(myBitmap); 

public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth,
            int reqHeight) {

    Bitmap bm = null;

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, options);

    return bm;
}

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
         inSampleSize = Math.round((float)height / (float)reqHeight);    
        } else {
         inSampleSize = Math.round((float)width / (float)reqWidth);    
        }   
       }

【问题讨论】:

标签: android


【解决方案1】:

当你完成了你的位图,意味着当你的位图完成了它的工作,然后让它像下面这样 recyle 和 null:

bitmap.recycle();
bitmap=null;

我认为你是从 url 下载图片,所以我建议你使用 Android Query,如果你使用它,你将永远不会得到这个错误。

您可以从这里下载 jar 文件: http://code.google.com/p/android-query/downloads/list 下载 jar 文件并将 jar 设置为您的构建路径。

 AQuery androidAQuery=new AQuery(this);

以直接从url加载图片为例:

androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);

以从 url 获取 Bitmap 为例:

androidAQuery.ajax(YOUR IMAGE URL,Bitmap.class,0,new AjaxCallback<Bitmap>(){
    @Override
    public void callback(String url, Bitmap object, AjaxStatus status) {
        super.callback(url, object, status);

        //You will get Bitmap from object.
    }
});

它非常快速和准确,使用它您可以在加载时找到更多功能,例如动画;如果需要,获取位图;等等

【讨论】:

  • 我已经下载了 android-query.0.25.10.jar,我在 AjaxStatus 中遇到了错误。你能帮我解决这个问题吗
  • thnaks 它的工作:) 并且与其他代码相比它更快。
  • 我必须在哪里使用此代码?我使用了这个 jar 文件。
  • 在你需要的地方,只需创建一个方法并调用此代码,将您的图片 url 传递给此方法即可。
  • @pratt 在滚动加载超过 500 张图像时,我仍然遇到此错误。 AQ 中任何选项的任何建议或设置。
【解决方案2】:

现在您的图像尺寸很大,为什么要使用这样的宽度和高度,并且在设置图像后清除 chache

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 60, 60);
  img_cook[index].setImageBitmap(myBitmap);  
  if (myBitmap != null)
   {
     bitmap.recycle();
     bitmap = null;
     System.gc();
   }

【讨论】:

  • 给出错误“画布:尝试使用回收的位图”
【解决方案3】:

我猜您在创建第一个位图后没有收到 OOM 异常,而是在您将多个位图加载到内存后发生这种情况?

尝试通过在不再需要的位图上手动调用 recycle() 来提高效率。当 GC 收集 Bitmap 的一些数据并释放所有引用时,图像的实际内存存储在本机内存中,因此需要调用 bitmap.recycle() 来释放此内存,当您需要释放它时.

希望这会有所帮助。

【讨论】:

    【解决方案4】:

    Android 应用程序的内存量非常低。您应该小心管理它以避免内存不足异常。你可以看到 Google's Solution

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      相关资源
      最近更新 更多