首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >附加图像时出错,内存不足

附加图像时出错,内存不足
EN

Stack Overflow用户
提问于 2012-02-29 13:30:26
回答 2查看 89关注 0票数 0

这是我的代码。

代码语言:javascript
运行
复制
case RESULT_MEMORY_SELECT:       //SELECTING IMAGE FROM SD CARD
                 Uri photoUri = data.getData();
                 String[] filePathColumn = {MediaStore.Images.Media.DATA};
                 Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
                    if (cursor.moveToFirst())
                    {
                        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                        String filePath = cursor.getString(columnIndex);
                        cursor.close();
                        Bitmap imageReturned = BitmapFactory.decodeFile(filePath);
                        showViewOfReceiptInLayout(imageReturned);
                    }
                    break;


public void showViewOfReceiptInLayout(Bitmap imageBitmap)
        {
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
             imageSelected = baos.toByteArray();  // imageSelected is byteArray and i am storing this byte array in Database
             imageBitmap = Bitmap.createScaledBitmap(imageBitmap, 72, 72, false);
             image.setVisibility(View.VISIBLE); // For Visible 
             image.setImageBitmap(imageBitmap);


02-29 10:30:44.496: E/AndroidRuntime(7682): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-29 10:30:44.496: E/AndroidRuntime(7682):     at android.graphics.Bitmap.nativeCreate(Native Method)
02-29 10:30:44.496: E/AndroidRuntime(7682):     at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
02-29 10:30:44.496: E/AndroidRuntime(7682):     at android.graphics.Bitmap.createBitmap(Bitmap.java:435)

有谁能告诉我如何修复这个错误吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-29 13:56:53

以下是您必须缩小图像大小以避免内存不足的解决方案。

代码语言:javascript
运行
复制
 BitmapFactory.Options options=new BitmapFactory.Options();
        options.inSampleSize = 8;
        options.inJustDecodeBounds = true;

        Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);

        final int REQUIRED_SIZE=70;
        int width_tmp=options.outWidth, height_tmp=options.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        Bitmap btm=BitmapFactory.decodeStream(is, null, o2);
       img_t.setImageBitmap(btm);
票数 0
EN

Stack Overflow用户

发布于 2012-02-29 13:57:39

首先,你需要找到面向对象的原因:

如果你的应用程序在解码图像之前内存使用量很大,所以在解码图像之前你需要释放一些内存,如果你不使用对象,就释放对象引用。

您可以使用:adb shell dumpsys meminfo your_package_name来显示内存使用情况

如果解码图像之前应用程序内存使用量较小,则oom是解码图像发生的原因,如果发生这种情况,建议:

1.如果您的图像较大,在解码时,提供一个BitmapFactory.Option,该选项有一个inSampleSize来使用较小的内存来解码图像

2.在decode方法周围创建try块,捕获OutOfMemroyError

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9494034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档