当我尝试从相机或图库中获取图像时,出现错误.这是logcat的一部分:
06-27 05:51:47.297: E/dalvikvm-heap(438): Out of memory on a 35295376-byte allocation.06-27 05:51:47.312: E/dalvikvm(438): Out of memory: Heap Size=108067KB, Allocated=71442KB, limit=131072KB06-27 05:51:47.312: E/dalvikvm(438): Extra info: Footprint=108067KB, Allowed Footprint=108067KB, Trimmed=56296KB06-27 05:51:47.312: E/PowerManagerService(438): Excessive delay when setting lcd brightness: mLcdlight.setBrightness(176, 1) spend 288ms, mask=206-27 05:51:48.052: E/dalvikvm-heap(4332): Out of memory on a 24023056-byte allocation.06-27 05:51:48.057: E/dalvikvm(4332): Out of memory: Heap Size=63139KB, Allocated=40922KB, limit=65536KB06-27 05:51:48.057: E/dalvikvm(4332): Extra info: Footprint=63139KB, Allowed Footprint=63139KB, Trimmed=0KB06-27 05:51:48.057: E/EmbeddedLogger(438): App crashed! Process: <my_app_name>
这是我的代码,可让我拍摄图像:
Intent pickIntent = new Intent();pickIntent.setType("image/*");pickIntent.setAction(Intent.ACTION_GET_CONTENT);Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);Intent chooserIntent = Intent.createChooser(pickIntent, "Select or take a new Picture");chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { takePhotoIntent });startActivityForResult(chooserIntent, selectPic);
在onActivityResult()上,我这样做:
Bitmap bitmapSelectedImage = null;Uri selectedImage = data.getData();String[] filePathColumn = { MediaStore.Images.Media.DATA };Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);cursor.movetoFirst();String filePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));cursor.close();bitmapSelectedImage = BitmapFactory.decodefile(filePath); // Here is where do I get error.
我在bitmapSelectedImage = BitmapFactory.decodefile(filePath);上收到错误线.
我已经找到了很多网站/主题,但没人能帮上忙.
有什么建议么?
解决方法:
您的内存分配堆大小非常有限.
尝试将高分辨率图像从文件加载到堆很容易导致内存不足错误.
假设相机应用程序确实具有很高的分辨率(几乎可以肯定是这种情况),则应以显示所需的大小将位图的缩放版本加载到内存中.
已经建议您查看的文档-http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
提供了完整的功能方法来做到这一点.
1)第一步是计算(不加载到内存中)所需的比例.
那是calculateInSampleSize方法.
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWIDth, int reqHeight) { // Raw height and wIDth of image final int height = options.outHeight; final int wIDth = options.outWIDth; int inSampleSize = 1; if (height > reqHeight || wIDth > reqWIDth) { // Calculate ratios of height and wIDth to requested height and // wIDth final int heightRatio = Math.round((float)height / (float)reqHeight); final int wIDthRatio = Math.round((float)wIDth / (float)reqWIDth); // Choose the smallest ratio as inSampleSize value, this will // guarantee // a final image with both dimensions smaller than or equal to the // requested height and wIDth. inSampleSize = heightRatio < wIDthRatio ? heightRatio : wIDthRatio; } return inSampleSize;}
2)第二步是使用步骤1的完整方法:
public static Bitmap getSampleBitmapFromfile(String bitmapfilePath, int reqWIDth, int reqHeight) { // calculating image size BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(new fileinputStream(new file(bitmapfilePath)), null, options); int scale = calculateInSampleSize(options, reqWIDth, reqHeight); BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; return BitmapFactory.decodeStream(new fileinputStream(new file(bitmapfilePath)), null, o2);}
reqHeight和reqWith是显示图像的图像视图的高度和宽度(以像素为单位).
因此,假设您的图片视图为100×100像素,您所需要做的就是:
bitmapSelectedImage = getSampleBitmapFromfile(filePath, 100, 100);
总结 以上是内存溢出为你收集整理的Android-位图-内存不足异常全部内容,希望文章能够帮你解决Android-位图-内存不足异常所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)