相关疑难解决方法(0)

如何在Android中加载大图像并避免内存不足错误?

我正在开发一个使用大图像(1390×870:150kb - 50kb)的应用程序.当我点击触发器/ ImageView时,我正在添加图像.

在某一点上,我得到一个内存不足的错误:

java.lang.OutOfMemoryError
E/AndroidRuntime(23369): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime(23369): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:613)
E/AndroidRuntime(23369): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:378)
Run Code Online (Sandbox Code Playgroud)

为了调整图像大小,我正在这样做:

Bitmap productIndex = null;
final String imageLoc = IMAGE_LOCATION;
InputStream imageStream;
try {
     imageStream = new FileInputStream(imageLoc);
     productIndex = decodeSampledBitmapFromResource(getResources(), imageLoc, 400, 400);

     productIV.setImageBitmap(productIndex);
     } catch (FileNotFoundException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
     }
}


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(); …
Run Code Online (Sandbox Code Playgroud)

android bitmap out-of-memory

16
推荐指数
3
解决办法
4万
查看次数

标签 统计

android ×1

bitmap ×1

out-of-memory ×1