BitmapFactory.decodeFile 在模拟器上工作正常,但在 HTC 希望上失败
我不明白这个问题,为什么 BitmapFactory.decodeFile 在模拟器上工作正常,但在 HTC 希望上失败。代码如下:
File f = new File("/sdcard/DCIM/100MEDIA/");
File files[] = f.listFiles();
Bitmap b1 = null, b2 = null;
b1 = BitmapFactory.decodeFile(files[0].getPath());
b2 = BitmapFactory.decodeFile(files[1].getPath());
很简单,但是应用程序在 HTC 希望上意外停止。 哦,我发现问题了:
07-18 08:01:45.634: ERROR/dalvikvm-heap(20637): 8045568-byte external allocation too large for this process.
07-18 08:01:45.634: ERROR/(20637): VM won't let us allocate 8045568 bytes
07-18 08:01:45.743: ERROR/AndroidRuntime(20637): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
图片太大,怎么处理?我也需要高质量的图像。
I don't understand the problem, why BitmapFactory.decodeFile works fine on emulator but fails on HTC desire. The following code:
File f = new File("/sdcard/DCIM/100MEDIA/");
File files[] = f.listFiles();
Bitmap b1 = null, b2 = null;
b1 = BitmapFactory.decodeFile(files[0].getPath());
b2 = BitmapFactory.decodeFile(files[1].getPath());
It's simple, but the application stops unexpectedly on HTC desire.
Oh, I find the problem:
07-18 08:01:45.634: ERROR/dalvikvm-heap(20637): 8045568-byte external allocation too large for this process.
07-18 08:01:45.634: ERROR/(20637): VM won't let us allocate 8045568 bytes
07-18 08:01:45.743: ERROR/AndroidRuntime(20637): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
The images are too large, how to handle them? I need good quality images too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用外部路径的推荐方式是 getExternalStorageDirectory
将您的代码更改为类似这样的内容。
The recommended way of using external path is getExternalStorageDirectory
Change your code to something like this.
解码文件时使用 BitmapFactory.Options 对象。根据您的屏幕分辨率将 inSampleSize 设置为 2 或 4 之类的值,以获得最佳预览。它的作用是对图像进行下采样,这样就不会占用太多内存。
Use a BitmapFactory.Options object while decoding the file. Set inSampleSize to something like 2 or 4 depending on your screen resolution to get an optimum preview. What it does is that it downsamples the image so that it does not take too much memory.