【发布时间】:2011-02-17 10:21:03
【问题描述】:
我正在尝试制作一个显示来自相机的图像的基本应用程序,但是当我尝试使用 BitmapFactory.decodeFile 从 sdcard 加载 .jpg 时,它返回 null。
它不会给出我觉得奇怪的内存不足错误,但完全相同的代码在较小的图像上也能正常工作。
通用图库如何以如此少的内存显示来自相机的巨大图片?
【问题讨论】:
我正在尝试制作一个显示来自相机的图像的基本应用程序,但是当我尝试使用 BitmapFactory.decodeFile 从 sdcard 加载 .jpg 时,它返回 null。
它不会给出我觉得奇怪的内存不足错误,但完全相同的代码在较小的图像上也能正常工作。
通用图库如何以如此少的内存显示来自相机的巨大图片?
【问题讨论】:
尝试设置inSampleSize如this example所示。
【讨论】:
inSampleSize,首先使用inJustDecodeBounds 并将其与您的可用空间进行比较。
【讨论】:
经过大量工作后,我发现问题不在于代码,而在于模拟器的 ram 大小,编辑 avd 和增加 ram 大小可以解决所有问题并轻松保存巨大的图片。谢谢。
【讨论】:
发送:
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
int REQUIRED_SIZE = 640;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while(true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);
ByteArrayOutputStream bs2 = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, bs2);
getIntent().putExtra("byte_picture", bs2.toByteArray());
接收:
Bitmap photo = BitmapFactory.decodeByteArray(data.getByteArrayExtra("byte_picture"),0,data.getByteArrayExtra("byte_picture").length);
【讨论】:
这是另一个使用 inSampleSize 并根据可用内存动态确定要使用的图像分辨率的解决方案:
http://bricolsoftconsulting.com/handling-large-images-on-android/
【讨论】: