1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
package com.hundsun.zhoujl.android; import java.io.File; import java.util.ArrayList; import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; public class Test_fileActivity extends ListActivity { private ArrayList<String> items = null ; private ArrayList<String> paths = null ; private String rootPath = "/" ; private TextView mPath; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); mPath = (TextView)findViewById(R.id.mPath); mPath.setTextColor(Color.RED); getFileDir(rootPath); } private void getFileDir(String filePath) { mPath.setText(filePath); items = new ArrayList<String>(); paths = new ArrayList<String>(); File file = new File(filePath); File[] files = file.listFiles(); if (!filePath.equals(rootPath)) { items.add( "Back To " + rootPath); paths.add(rootPath); items.add( "Back to ../" ); paths.add(file.getParent()); } for (File fileTemp :files) { items.add(fileTemp.getName()); paths.add(fileTemp.getPath()); } ArrayAdapter<String> adapter = new ArrayAdapter<String>(Test_fileActivity. this ,R.layout.file_now,items); setListAdapter(adapter); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { File file = new File(paths.get(position)); if (file.canRead()) { if (file.isDirectory()) { getFileDir(paths.get(position)); } else { new AlertDialog.Builder( this ) .setTitle( "Message" ) .setMessage( "[" +file.getName() + "] is a file" ) .setPositiveButton( "ok" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); } } else { new AlertDialog.Builder( this ) .setTitle( "Message" ) .setMessage( "權限不足~" ) .setPositiveButton( "ok" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); } } } |
main.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<? xml version = "1.0" encoding = "utf-8" ?> android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < TextView android:id = "@+id/mPath" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/hello" /> < ListView android:id = "@android:id/list" android:layout_width = "fill_parent" android:layout_height = "wrap_content" > </ ListView > </ LinearLayout > |
file_now.xml
1
2
3
4
5
6
7
8
9
10
|
<? xml version = "1.0" encoding = "utf-8" ?> < TextView android:orientation = "vertical" android:layout_width = "match_parent" android:layout_height = "20px" android:textSize = "14sp" > </ TextView > |