如何在没有OutOfMemory和“Bitmap太大而无法上传到纹理(1840x3264,max = 2048x2048)”的情况下从磁盘下载照片作为图标

时间:2014-09-17 05:25:50

标签: android bitmap out-of-memory

我想从您的磁盘下载照片。我以前有一个错误OutOfMemory。我应对这个错误,但现在在某些手机上我收到错误“位图太大而无法上传到纹理(1840x3264,max = 2048x2048)”。在这种情况下,当图片上传时,我的应用程序开始变慢,生涩,慢下来。请告诉我如何从磁盘上传图片以避免这些错误并表达

2 个答案:

答案 0 :(得分:1)

使用此方法创建位图 -

 Bitmap bm=decodeSampledBitmapFromPath(src, reqWidth, reqHeight);
 // might be your Screen Height and Width in your Case

使用此定义 -

public Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
    int reqHeight) {

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);

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

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeFile(path, options);
return bmp;
}
}
  public 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);
     }
 }
 return inSampleSize;
}

注意 - 根据您的sceenSize 制作您的reqWidth和reqHeight。

答案 1 :(得分:0)

您无法动态增加堆大小。

您可以使用

请求使用更多内容
 android:largeHeap="true"
清单中的

另外,您可以使用本机内存(NDK和JNI),因此您实际上绕过了堆大小限制。

这里有一些关于它的帖子:

这是一个为它制作的图书馆:

快乐编码

关于maven