Therefore, we write the files to Explorer, right!
First, we place a ListView in the xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http: //schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/listView" /> </LinearLayout>
When we search the directory listing of the layers in the ListViewWe declare an ArrayList, used to hold pictures and names under a parent directory of each data
Then fitted with two ArrayList each data name and path.
List<Map<String, Object>> filesList = new ArrayList<Map<String,Object>>(); filesList.clear(); names = new ArrayList<String>(); paths = new ArrayList<String>(); File[] files = new File(filePath).listFiles(); Map<String, Object> filesMap;
Begin to determine whether to enter the root directory, if not into the root directory,
Then they would join the two fields, one is back to the root directory (.) And a return to the previous (..)
if(!filePath.equals("/")){ filesMap = new HashMap<String, Object>(); names.add("."); paths.add("/"); filesMap.put("image", fileImg[0]); filesMap.put("text", "."); filesList.add(filesMap); filesMap = new HashMap<String, Object>(); names.add(".."); paths.add(new File(filePath).getParent()); filesMap.put("image", fileImg[0]); filesMap.put("text", ".."); filesList.add(filesMap); }
private int[] fileImg = { R.drawable.directory, R.drawable.file }; private SimpleAdapter simpleAdapter;
we are also using the same way to create SimpleAdapter
And then thrown into the ListView, and can be completed with a icon ListView
Which to determine whether the data for the directory, if it is, we use the folder icon.
If not, we will use the file icon.
for(int i=0; i<files.length; i++){ filesMap = new HashMap<String, Object>(); names.add(files[i].getName()); paths.add(files[i].getPath()); if(files[i].isDirectory()){ filesMap.put("image", fileImg[0]); } else if(files[i].isFile()){ filesMap.put("image", fileImg[1]); } filesMap.put("text", files[i].getName()); filesList.add(filesMap); } simpleAdapter = new SimpleAdapter(this, filesList, R.layout.simple_adapter, new String[]{"image", "text"}, new int[]{R.id.image, R.id.text}); listView.setAdapter(simpleAdapter);
Run the program, you will see a directory of the ListView.
But we must also implement a folder when the user points
Will jump down a layer of functionality.
listView.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position,long id) { File file = new File(paths.get(position)); if(file.canRead()){ if(file.isDirectory()){ getFileDirectory(paths.get(position)); } } } });
Our Android is real on Linux file permissions are more stringent
Therefore, we must determine the folder access permissions,
If we're on the directory passed in front of the function.
Code
http://uploadingit.com/file/iogovank4rfsjwxl/FileManagerDemo.zip