Gridview如何显示本地存储路径的图片

如下是我现在GridView加载图片和文字的方式,如果图片不在资源里面,存在手机本地路径,要怎么改才能加载图片。
自己试了试发现把map.put("img", img[i]);改成map.put("img", Uri.parse("file://" + file));就可以,直接把文件路径放进去。虽然我也不知道这样做的原理是什么,有人解惑么。

 public class MainActivity extends AppCompatActivity {

    private GridView grid_test;
    private List<Map<String,Object>> dataList;
    private SimpleAdapter simpleAdapter;
    private int[] img = {R.mipmap.ic_launcher_round,R.mipmap.ic_launcher,R.mipmap.ic_launcher_round, R.mipmap.ic_launcher,
            R.mipmap.ic_launcher_round,R.mipmap.ic_launcher,R.mipmap.ic_launcher_round,R.mipmap.ic_launcher,
            R.mipmap.ic_launcher_round,R.mipmap.ic_launcher,R.mipmap.ic_launcher_round,R.mipmap.ic_launcher,
            R.mipmap.ic_launcher_round,R.mipmap.ic_launcher,R.mipmap.ic_launcher_round,R.mipmap.ic_launcher,
            R.mipmap.ic_launcher_round,R.mipmap.ic_launcher,R.mipmap.ic_launcher_round,R.mipmap.ic_launcher,
            R.mipmap.ic_launcher_round,R.mipmap.ic_launcher,R.mipmap.ic_launcher_round,R.mipmap.ic_launcher};
    private  String[] imgName = {"1","2","3","4",
            "5","6","7","8",
            "9","10","11","12",
            "13","14","15","16",
            "17","18","19","20",
            "21","22","23","24"};


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

        grid_test = (GridView) findViewById(R.id.grid_test);
        dataList = new ArrayList<Map<String, Object>>();
        simpleAdapter = new SimpleAdapter(this,getData(),R.layout.grid_item,
                new String[] {"img","txt"},new int[]{R.id.img_item,R.id.txt_item});
        grid_test.setAdapter(simpleAdapter);
    }

    private  List<Map<String,Object>> getData() {
        for (int i=0; i<img.length; i++) {
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("img", img[i]);
            map.put("txt", imgName[i]);
            dataList.add(map);
        }

        return dataList;
    }
}

7个回答

这个很简单,如果改成map.put("img", Uri.parse("file://" + file)); SimpleAdatper绑定数据的时候会调用ImageView的setImageURI(),将你的uri传进去,
ImageView会自动从文件里解析流还获取对应的Bitmap。

从数据库读出你的路径,添加到GridView'中的图片列中DataImageUrlField列上.

通过适配器来SimpleAdapter dAdapter = new SimpleAdapter(MainActivity.this,
listmage, R.layout.activity_main, from, to);//listmage是arrylist数字保存的路径,然后设置显示的用ImageView在布局文件添加,然后设置int to[]={R.id.ImageView};

只要知道本地手机每张图片的路径phonePicPath,每张图片都可以Bitmap bitmap = BitmapFactory.decodeFile(phonePicPath);

String img_url = uri.getPath();//这是本机的图片路径
ContentResolver cr = this.getContentResolver();
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
imageView.setImageBitmap(bitmap);

自己试了试发现把map.put("img", img[i]);改成map.put("img", Uri.parse("file://" + file));就可以,直接把文件路径放进去。虽然我也不知道这样做的原理是什么,有人解惑么。

 public class GridActivity extends Activity {

    List<MyClass> list = new ArrayList<MyClass>();

    private GridView grid_test;

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

        initList();

        grid_test = (GridView) findViewById(R.id.grid_test);
        grid_test.setAdapter(new MyAdapter());
    }

    private void initList() {
        list.add(new MyClass("1", getBMP("test.png")));
        list.add(new MyClass("2", getBMP("test.png")));
        list.add(new MyClass("3", getBMP("test.png")));
        list.add(new MyClass("4", getBMP("test.png")));
        list.add(new MyClass("5", getBMP("test.png")));
        list.add(new MyClass("6", getBMP("test.png")));
        list.add(new MyClass("7", getBMP("test.png")));
        list.add(new MyClass("8", getBMP("test.png")));
        list.add(new MyClass("9", getBMP("test.png")));
    }

    private Bitmap getBMP(String file) {
        try {
            FileInputStream is = new FileInputStream(Environment.getExternalStorageDirectory() + "/" + file);
            Bitmap bmp = BitmapFactory.decodeStream(is);

            return bmp;
        } catch (Exception e) {
        }

        return null;
    }

    private class MyClass {
        String name;
        Bitmap img;

        public MyClass(String name, Bitmap img) {
            this.name = name;
            this.img = img;
        }
    }

    private class MyAdapter extends BaseAdapter {

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

        @Override
        public Object getItem(int arg0) {
            return null;
        }

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

        @Override
        public View getView(int position, View arg1, ViewGroup arg2) {
            View view = getLayoutInflater().inflate(R.layout.grid_item, null);

            TextView tv = (TextView) view.findViewById(R.id.txt_item);
            tv.setText(list.get(position).name);
            ImageView img = (ImageView) view.findViewById(R.id.img_item);
            img.setImageBitmap(list.get(position).img);

            return view;
        }

    }
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!