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 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题