手机显示linux file too largee to be load怎么办

2263人阅读
开发心得和一些总结(17)
分类:&&408人阅读&&&
安卓Bitmap too large to be uploaded into a texture (, max=
BitmapFactory.decodeFile(imageFile);
用BitmapFactory解码一张图片时,有时会遇到该错误。这往往是由于图片过大造成的。要想正常使用,则需要分配更少的内存空间来存储。
BitmapFactory.Options.inSampleSize
设置恰当的inSampleSize可以使BitmapFactory分配更少的空间以消除该错误。inSampleSize的具体含义请参考SDK文档。例如:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
设置恰当的inSampleSize是解决该问题的关键之一。BitmapFactory.Options提供了另一个成员inJustDecodeBounds。
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds =
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,即opts.width和opts.height。有了这两个参数,再通过一定的算法,即可得到一个恰当的inSampleSize。
查看Android源码,我们得知,为了得到恰当的inSampleSize,Android提供了一种动态计算的方法。
public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
int roundedS
if (initialSize &= 8) {
roundedSize = 1;
while (roundedSize & initialSize) {
roundedSize &&= 1;
roundedSize = (initialSize + 7) / 8 * 8;
return roundedS
private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
double w = options.outW
double h = options.outH
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound & lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerB
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
} else if (minSideLength == -1) {
return lowerB
return upperB
使用该算法,就可动态计算出图片的inSampleSize。
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds =
BitmapFactory.decodeFile(imageFile, opts);
opts.inSampleSize = computeSampleSize(opts, -1, 128*128);
opts.inJustDecodeBounds =
Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts);
imageView.setImageBitmap(bmp);
} catch (OutOfMemoryError err) {
综合上述,完整代码是:
转载地址:http://blog.csdn.net/hustpzb/article/details/8363372
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:42398次
排名:千里之外
原创:30篇
转载:26篇
评论:12条
(2)(2)(1)(2)(1)(1)(2)(2)(4)(2)(1)(4)(3)(1)(1)(3)(1)(3)(2)(1)(17)

我要回帖

更多关于 cp file too large 的文章

 

随机推荐