【问题标题】:Am Getting OutOfMemoryError when trying to load images in my widget尝试在我的小部件中加载图像时出现 OutOfMemoryError
【发布时间】:2015-07-29 03:07:57
【问题描述】:

您好,我收到以下错误,当我尝试启动我的小部件时,它是一个笔记应用程序,这是我得到的错误,请使用我的代码提供一个可行的答案,很乐意接受有效的答案,谢谢提前:-

05-16 02:53:42.491: E/AndroidRuntime(5335): java.lang.OutOfMemoryError

并且错误指向我的代码中的这一行:-

 File imgFile = new File(data.get(position).get("path"));
 Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
 imageView.setImageBitmap(myBitmap);

【问题讨论】:

标签: android memory-management bitmap out-of-memory bitmapimage


【解决方案1】:

您可以使用Picasso 为您处理这一切,我认为这是强烈推荐的。就像包含库并调用它一样简单:

Picasso.with(context).load(myBitmap).into(imageView);

或者,您可以尝试link 中列出的方法。

具体来说,就是这个。

public static Bitmap decodeSampledBitmapFromResource(Resources res, String resId, int    reqWidth, int reqHeight) {

 // First decode with inJustDecodeBounds=true to check dimensions
 final BitmapFactory.Options options = new BitmapFactory.Options();
 options.inJustDecodeBounds = true;
 BitmapFactory.decodeFile(resId, options);

 // Calculate inSampleSize
 options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

 // Decode bitmap with inSampleSize set
 options.inJustDecodeBounds = false;
 options.inPreferredConfig = Config.RGB_565; <---- the options and the one below are important
 options.inDither = true;
 return BitmapFactory.decodeFile(resId, options);
 }

【讨论】:

  • 尝试将 (context) 更改为 (getApplicationContext())
  • 当我更改为 (getApplicationContext()) 时,我现在在 .load 上遇到错误
【解决方案2】:

这可能会帮助那里的人,几个小时后没有答案。我找到了一种处理 outOfMemoryError 的方法。我在 Manifest 文件中添加了这行代码。

android:largeHeap="true"

我在这里将它添加到整个应用程序中,如下所示:-

<application
  android:icon="@drawable/ic_launcher"
  android:largeHeap="true"
  android:label="@string/app_name" >

android:largeHeap 是为应用增加分配内存的工具。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 2014-08-13
    • 2014-09-07
    • 2013-01-29
    • 2013-02-23
    相关资源
    最近更新 更多