在GridView中设置特定的图像

时间:2019-06-29 14:55:42

标签: java android

因此,我想创建一个显示存储图像的gridview,以便以后将其用作消息(如Messenger中所示) 目前可以使用,但它会显示设备中的每个图像,例如加载的图标和东西,我希望它仅显示图库中可用的图像

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);


    list = getImageFiles(Environment.getExternalStorageDirectory());

    gridView = (GridView) findViewById(R.id.gridView);
    gridView.setAdapter(new GridAdapter());

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            startActivity(new Intent(getApplicationContext(), ViewImage.class).putExtra("img", list.get(i).toString()));
        }
    });

}

class GridAdapter extends BaseAdapter {

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = getLayoutInflater().inflate(R.layout.griditem, viewGroup, false);
        ImageView imageView = (ImageView) view.findViewById(R.id.specialImageView);


        Glide.with(getApplicationContext())
                .load(getItem(i).toString())
                .into(imageView);
        return view;
    }
}

ArrayList<File> getImageFiles(File root) {
    ArrayList<File> list = new ArrayList();
    File[] files = root.listFiles();
    for (int i = 0; i < files.length; i++) {
        if(files[i].isDirectory()) {
            list.addAll(getImageFiles(files[i]));
        } else {
            if(files[i].getName().endsWith(".jpg") || files[i].getName().endsWith(".png")) {
                list.add(files[i]);
            }
        }
    }
    return  list;
}

我希望gridview不加载所有图像,而是加载图库中的图像(而不是图标和其他网站/应用程序加载的图像),谢谢!

0 个答案:

没有答案