Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中bitmap factory.decode文件中的java.lang.outofmemory错误_Android_Bitmap - Fatal编程技术网

Android中bitmap factory.decode文件中的java.lang.outofmemory错误

Android中bitmap factory.decode文件中的java.lang.outofmemory错误,android,bitmap,Android,Bitmap,首先,我调用MediaStore.ACTION\u IMAGE\u CAPTURE intent打开照相机,然后从该函数中获取保存的捕获图像路径 private String getLastImageId(){ String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA }; String imageOrderBy = MediaStore.Images.Media._ID

首先,我调用MediaStore.ACTION\u IMAGE\u CAPTURE intent打开照相机,然后从该函数中获取保存的捕获图像路径

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 "";
    }
}
然后我将该路径作为参数传递给产品对象属性。 将该对象添加到产品列表中 该列表由自定义适配器显示。 我正在列表视图中显示产品标题和图像

在定制适配器类中,我从中获取产品名称和路径 从路径生成位图,并将其指定给包含图像视图的产品支架 用这种方法处理第一张照片很好,但在第二张照片上,它给出了java.lang.outofmemory的例外,我也尝试了在

在产品适配器中这样做

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;
}
}
公共类ProductAdapter扩展了ArrayAdapter{
语境;
国际布局资源;
ArrayList数据=null;
公共产品采用者(上下文、int-layoutResourceId、ArrayList产品_数据){
//TODO自动生成的构造函数存根
超级(上下文、布局资源ID、产品数据);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
this.data=产品数据;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
ProductHolder=null;
if(行==null)
{
LayoutInflater充气器=((MainActivity)上下文)。getLayoutInflater();
行=充气机。充气(layoutResourceId,父级,false);
holder=新产品holder();
holder.imgIcon=(ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle=(TextView)row.findViewById(R.id.txtTitle);
row.setTag(支架);
}
其他的
{
holder=(ProductHolder)行。getTag();
}
产品=数据。获取(位置);
holder.txtTitle.setText(product.getName());
File imgFile=新文件(product.icon);
if(imgFile.exists()){
位图myBitmap=BitmapFactory.decodeFile(imgFile.getAbsolutePath());
holder.imgIcon.setImageBitmap(myBitmap);
//myBitmap.recycle();
}
返回行;
}
静态类产品持有者
{
ImageView imgIcon;
TextView-txtTitle;
}
}

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

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


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

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

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

欲了解更多信息,请参阅

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

有关更多信息,请参见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);
BitmapFactory.Options op=new-BitmapFactory.Options();
op.inSampleSize=8;
返回BitmapFactory.decodeFile(“,op”);
如果您有许多图像,上述组合可能会很有用。

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);
BitmapFactory.Options op=new-BitmapFactory.Options();
op.inSampleSize=8;
返回BitmapFactory.decodeFile(“,op”);
如果您有许多图像,上述组合可能很有用