Dialog中的Gallery,OutOfMemoryError

时间:2017-05-17 06:20:29

标签: android bitmap out-of-memory gallery thumbnails

我创建了简单的图库但是当我尝试在几秒钟后显示它时,可见应用程序崩溃并且我收到错误:

Throwing OutOfMemoryError "Failed to allocate a 31961100 byte allocation with 16777216 free bytes and 19MB until OOM"

我必须承认当我尝试在图库中只显示一张尺寸小于1MB的照片时会发生这种情况

可能这一切都发生了,因为我得到这个.jpg文件,应该解码Bitmap?或者因为我没有创建它的缩略图?还有一张照片需要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();
    }

GridAdapter:

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;
    }
}

使用代码执行此操作:

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;
    }

P.S抱歉语言软弱无力。

1 个答案:

答案 0 :(得分:0)

使用Glide库在Gallery中显示图像 https://github.com/bumptech/glide