【问题标题】:java.lang.outofmemory error at bitmap factory.decodefile in AndroidAndroid 中 bitmapfactory.decodefile 中的 java.lang.outofmemoryerror
【发布时间】:2013-04-25 02:53:51
【问题描述】:

首先我调用 MediaStore.ACTION_IMAGE_CAPTURE 意图打开相机,然后我从这个函数中获取保存的捕获图像路径。

private String getLastImageId(){
    String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
    String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
    Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
    if(imageCursor.moveToFirst()){
        int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
        String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
        //imageCursor.close();
        return fullPath;
    }else{
        return "";
    }
}

然后我将该路径作为参数传递给产品对象属性。 将该对象添加到产品列表 该列表由定制的适配器显示。 我在 listView 中显示产品标题和图像

在自定义适配器类中,我从中获取产品获取产品标题和路径 从路径制作位图并将其分配给包含图像视图的产品持有人 以这种方式处理第一张照片很好,但在第二张照片上它给出了 java.lang.outofmemory 的例外我也尝试了在

java.lang.OutOfMemoryError: bitmap size exceeds VM budget - Android

在产品适配器中这样做

public class ProductAdopter extends ArrayAdapter<Product> {
Context context; 
int layoutResourceId;    
ArrayList<Product> data = null;
public ProductAdopter(Context context, int layoutResourceId, ArrayList<Product> product_data) {
    // TODO Auto-generated constructor stub
    super(context, layoutResourceId, product_data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = product_data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ProductHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((MainActivity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ProductHolder();
        holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
        holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

        row.setTag(holder);
    }
    else
    {
        holder = (ProductHolder)row.getTag();
    }

    Product product = data.get(position);
    holder.txtTitle.setText(product.getName());
    File imgFile = new  File(product.icon);
    if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

        holder.imgIcon.setImageBitmap(myBitmap); 
        //myBitmap.recycle();
    }
    return row;
}
static class ProductHolder
{
    ImageView imgIcon;
    TextView txtTitle;
}
}

【问题讨论】:

    标签: android bitmap


    【解决方案1】:
     if(imgFile.exists()){
    
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
    
       Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(),options);
            holder.imgIcon.setImageBitmap(myBitmap); 
            //myBitmap.recycle();
        }
    

    使用 inSampleSize 将比例位图加载到内存中。对 inSampleSize 值使用 2 的幂对于解码器来说更快、更有效。但是,如果您计划将调整大小的版本缓存在内存或磁盘上,通常仍值得将其解码为最合适的图像尺寸以节省空间。

    更多信息请见Loading Large Bitmaps Efficiently

    【讨论】:

      【解决方案2】:

      如果您尝试自己解码大图像,则会遇到内存不足的问题。

      尝试使用通用图像加载器。它将负责从本地存储或互联网加载所有图像。

      https://github.com/nostra13/Android-Universal-Image-Loader

      【讨论】:

        【解决方案3】:

        OutOfMemory 可能很累人 这里有一些您可以查看的选项

        try { 
             //Create your bitmap here 
        
        } catch (OutOfMemoryError ooM) {
                // You got out of memory now do something to recycle images
                            // Yes you can catch OOM.
                recycle();
        }
        

        如果你没有太多的图片要处理,那么你可以试试这个。

            BitmapFactory.Options op = new BitmapFactory.Options();
            op.inSampleSize = 8;
            return BitmapFactory.decodeFile("<YOUR IMAGE FILE HERE>", op);
        

        如果您有很多图像,上述组合可能会很有用

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-15
          • 1970-01-01
          • 2016-11-12
          • 2015-07-03
          • 1970-01-01
          • 2017-04-18
          • 2014-10-23
          • 2017-05-19
          相关资源
          最近更新 更多