如下是我现在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;
}
}