Android 文件瀏覽器
16lz
2021-01-23
public class file extends Activity{private ListView listView;private TextView path;private File file_parent;private File[] files;private String root ="/";@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.file);listView=(ListView)findViewById(R.id.list);path=(TextView)findViewById(R.id.path);file_parent = new File(root);files = new File(root).listFiles();GetFileDir(files);listView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {// TODO Auto-generated method stubif (files[arg2].isFile()) {return;}File[] temp = files[arg2].listFiles();if (temp == null || temp.length == 0) {Toast.makeText(file.this, "当前路径不可访问或该路径下没有任何文件!", 1000).show();}else {file_parent = files[arg2];files = temp;GetFileDir(files);}}});}public void GetFileDir(File[] file){ArrayList<Map<String, Object>> list = new ArrayList<Map<String,Object>>();//Map<String, Object> map1 = new HashMap<String, Object>();//map1.put("icon", R.drawable.ic_launcher);//map1.put("name", "back to /");//list.add(map1);//Map<String, Object> map2 = new HashMap<String, Object>();//map2.put("icon", R.drawable.ic_launcher);//map2.put("name", "back to ../");//list.add(map2);for (int i = 0; i < file.length; i++) {Map<String, Object> map = new HashMap<String, Object>();if (file[i].isDirectory()) {map.put("icon", R.drawable.folder);}else {map.put("icon", R.drawable.file);}map.put("name", file[i].getName());list.add(map);}SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.line, new String[]{"icon","name"}, new int[]{R.id.icon,R.id.name});listView.setAdapter(simpleAdapter);try {path.setText("当前路径为:" + file_parent.getCanonicalPath());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
更多相关文章
- Android之通过ContentProvider共享文件
- Android保存Log信息到本地文件
- java和Android文件下载断点续传和图片下载代码实现,可直接复制简
- 列出sdcard里所有.mp3文件,并且可以点击播放
- Android手机开发——向SD卡上的文件追加内容
- Unity android 读取文件,读取APK包文件
- Android 用Socket实现PC和手机的文件传输