Android 库内对话框,OutOfMemoryError

Android 库内对话框,OutOfMemoryError,android,bitmap,out-of-memory,gallery,thumbnails,Android,Bitmap,Out Of Memory,Gallery,Thumbnails,我创建了simple gallery,但当我尝试在几秒钟后显示它时,它是可见的,应用程序崩溃,我得到错误: Throwing OutOfMemoryError "Failed to allocate a 31961100 byte allocation with 16777216 free bytes and 19MB until OOM" 我必须补充一点,当我试图在图库中只显示一张大小小于1MB的照片时,就会发生这种情况 可能所有这些都是因为我得到了这个.jpg文件,它应该被解码?或者是因为

我创建了simple gallery,但当我尝试在几秒钟后显示它时,它是可见的,应用程序崩溃,我得到错误:

Throwing OutOfMemoryError "Failed to allocate a 31961100 byte allocation with 16777216 free bytes and 19MB until OOM"
我必须补充一点,当我试图在图库中只显示一张大小小于1MB的照片时,就会发生这种情况

可能所有这些都是因为我得到了这个.jpg文件,它应该被解码?或者是因为我没有创建它的缩略图?还有一张照片拍了160RAM?!?!很奇怪。如果我想让它在超过1张图片的情况下快速平滑地工作,我应该怎么做:)。一些示例代码将非常好

下面是我的所有代码

对话框库:

public void openDialog() {

        inflater = this.getLayoutInflater();

        // Dialog layout
        v = inflater.inflate(R.layout.dialog_choice, null);

        // Get gridView from dialog_choice
        gV = (GridView) v.findViewById(R.id.gridView);

        // GridAdapter (Pass context and files list)
        GridAdapter adapter = new GridAdapter(this, photoList);

        // Set adapter
        gV.setAdapter(adapter);

        final AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
        builder2.setTitle("MY GALLERY");
        builder2.setView(v);
        builder2.setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).setNegativeButton("BACK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder2.setCancelable(false);
        builder2.create().show();
    }
网格适配器:

public class GridAdapter extends BaseAdapter {

    Context mContext;
    ArrayList<File> listFiles;

    public GridAdapter(Context context, ArrayList<File> files) {

        this.mContext = context;
        this.listFiles = files;
    }

    @Override
    public int getCount() {
        return listFiles.size();
    }

    @Override
    public Object getItem(int position) {
        return listFiles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null)
        {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.my_grid, parent, false);
        }

        ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);

        iv.setImageURI(Uri.parse(listFiles.get(position).toString()));

        return convertView;
    }
}
公共类GridAdapter扩展BaseAdapter{
语境;
ArrayList列表文件;
公共GridAdapter(上下文、ArrayList文件){
this.mContext=上下文;
this.listFiles=文件;
}
@凌驾
public int getCount(){
返回listFiles.size();
}
@凌驾
公共对象getItem(int位置){
返回listFiles.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null)
{
convertView=LayoutInflater.from(mContext).充气(R.layout.my_grid,parent,false);
}
ImageView iv=(ImageView)convertView.findViewById(R.id.ImageView);
iv.setImageURI(Uri.parse(listFiles.get(position.toString()));
返回视图;
}
}
Rest使用了以下代码:

public ArrayList<File> photoList;

  @Override public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide_photo_album);
        photoList = imageReader(my_root);
}


    private ArrayList<File> imageReader(File root)
    {
        ArrayList<File> a = new ArrayList<>();
        File[] files = root.listFiles();
        for(int i=0;i<files.length;i++)
        {
            if(files[i].isDirectory())
            {
                Bitmap bit = BitmapFactory.decodeFile(files[i].getAbsolutePath());
                a.addAll(imageReader(files[i]));
            }
            else
            {
                if (files[i].getName().endsWith(".jpg"))
                {
                    a.add(files[i]);
                }
            }
        }
        return a;
    }
公共阵列列表照片列表;
@在创建时覆盖公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.guide\u photo\u相册);
照片列表=图像阅读器(我的根);
}
专用ArrayList imageReader(文件根)
{
ArrayList a=新的ArrayList();
File[]files=root.listFiles();

对于(int i=0;i使用Glide库在库中显示图像

如何做所有这些事情,我应该在哪里实现它+仍然是同一个文件,所以仍然会有内存不足的问题。只是更快的显示/加载你认为这有帮助吗?简单的应用程序使用160mb内存,你的想法是允许应用程序获得更多内存亲爱的,请正确地写下你的问题,160R的意义是什么AM???。提及MB(例如:160MB RAM),以便观众能够正确理解。我有一个建议,学习如何使用毕加索或Glide或通用加载器制作图像库类型的应用程序,并学习如何通过编程压缩位图。