mynamecsl 2018-06-12 05:59 采纳率: 58%
浏览 1287
已采纳

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条回答 默认 最新

  • JabamiLight 2018-06-14 01:29
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog