每天一篇
博客,每天充实一点知识
2017年7月5日 安照Android疯狂讲义上写了一个SD卡文件浏览器 6日上午把这一篇博客写完
需求: 展示出当前文件夹的所以文件
点击当前文件夹下的文件刷新界面,地址转换为点击的文件夹下 展示出当前文件夹下的所有文件
点击会退 返回当上一级文件夹,并展示出当前文件夹下的所有文件
界面: 一个TextView 展示出当前地址
Button 点击返回上一级
ListView 展现当前文件夹下的所有文件
先准备Activity界面
1
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
android:layout_width="match_parent"
4
android:layout_height="match_parent"
5
android:orientation="vertical"
6
>
7
<TextView
8
android:text="当前路径"
9
android:id="@+id/tv_path"
10
android:layout_width="match_parent"
11
android:layout_height="wrap_content"
12
/>
13
14
<Button
15
android:id="@+id/bt_return"
16
android:text="返回上一级"
17
android:layout_width="match_parent"
18
android:layout_height="wrap_content" />
19
20
<ListView
21
android:id="@+id/lv_file"
22
android:layout_width="match_parent"
23
android:layout_height="match_parent"/>
24
25
</LinearLayout>
26
然后就是java代码
1
package toy.yacatool.com.mysdfolder;
2
3
import android.os.Environment;
4
import android.os.Handler;
5
import android.os.Message;
6
import android.support.v7.app.AppCompatActivity;
7
import android.os.Bundle;
8
import android.util.Log;
9
import android.view.View;
10
import android.view.ViewGroup;
11
import android.widget.AdapterView;
12
import android.widget.BaseAdapter;
13
import android.widget.Button;
14
import android.widget.ImageView;
15
import android.widget.ListView;
16
import android.widget.TextView;
17
import android.widget.Toast;
18
19
import java.io.File;
20
import java.util.ArrayList;
21
22
public class MainActivity extends AppCompatActivity {
23
24
private final String TAG = "MainActivity";
25
26
private TextView tv_path;
27
/**
28
* 当前文件路径
29
*/
30
private File currentParent ;
31
32
/**
33
* 当前路径的文件
34
*/
35
private File[] currentParents;
36
37
private ListView lv_file;
38
39
private File[] files;
40
private ArrayList<String> directoryList;
41
private ArrayList<String> matteryList;
42
43
private File file;;
44
45
private Handler mHandler = new Handler(){
46
47
public void handleMessage(Message msg) {
48
49
MyBaseAdapter adapter = new MyBaseAdapter();
50
lv_file.setAdapter(adapter);
51
tv_path.setText(currentParent.getName());
52
53
switch (msg.what){
54
case 1:
55
Toast.makeText(getApplicationContext(),"该文件为空",Toast.LENGTH_SHORT).show();
56
break;
57
}
58
59
}
60
};
61
62
63
protected void onCreate(Bundle savedInstanceState) {
64
super.onCreate(savedInstanceState);
65
setContentView(R.layout.activity_main);
66
67
//File sdCardDir = Environment.getExternalStorageDirectory();
68
//String sdPath = sdCardDir.getAbsolutePath(); 用代码获取SD卡的路径
69
final File root = new File("mnt/sdcard/");
70
//如果sd卡存在的话
71
if (root.exists()){
72
new Thread(){
73
//因为要读取数据,个人觉得是获取数据的操作基本是耗时操作,而这是获取SD卡的数据所以也归为耗时操作,使用子线程操作
74
75
public void run() {
76
currentParent = root;
77
//获取到SD卡为地址的所有文件
78
currentParents = root.listFiles();
79
//填充TextView 把当前路径填充进去
80
tv_path.setText(currentParent.getName());
81
int length = currentParents.length;
82
Log.w(TAG, "length: "+length);
83
isFile(currentParents);
84
//提醒Handler 刷新UI
85
mHandler.sendEmptyMessage(0);
86
}
87
}.start();
88
}
89
initView();
90
91
92
}
93
94
private void initView() {
95
tv_path = (TextView) findViewById(R.id.tv_path);
96
Button bt_return = (Button) findViewById(R.id.bt_return);
97
lv_file = (ListView) findViewById(R.id.lv_file);
98
99
//返回上一级
100
bt_return.setOnClickListener(new View.OnClickListener() {
101
102
public void onClick(View v) {
103
try {
104
//得到当前文件夹的名字
105
String canonicalPath = currentParent.getCanonicalPath();
106
Log.w(TAG,"前canonicalPath==================="+canonicalPath);
107
//如果不是在SD卡的顶层目录的话,就去取到该文件夹
108
if (!currentParent.getCanonicalPath().equals("/mnt/sdcard/")){
109
//获取上一层的数据
110
//接下来的就是同之前的操作一样
111
currentParent = currentParent.getParentFile();
112
Log.w(TAG,"后canonicalPath==================="+canonicalPath);
113
currentParents = currentParent.listFiles();
114
isFile(currentParents);
115
mHandler.sendEmptyMessage(0);
116
}
117
}catch (Exception e){
118
e.printStackTrace();
119
}
120
121
122
}
123
});
124
125
lv_file.setOnItemClickListener(new AdapterView.OnItemClickListener() {
126
127
public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
128
129
new Thread(){
130
131
public void run() {
132
133
File file;
134
/*
135
如果不是文件夹就返回teturn
136
如果为空文件夹 也不做任何事件
137
138
currentParent 当前路径要更改
139
140
*/
141
if (position > directoryList.size()-1){
142
return ;
143
}
144
145
for (int i = 0 ; i < currentParents.length ; i++){
146
if (currentParents[i].getName().equals((String)directoryList.get(position))){
147
currentParent = currentParents[i];
148
149
files = currentParents[i].listFiles();
150
}
151
}
152
153
if (files == null || files.length ==0){
154
Log.w(TAG,"此文件为空");
155
// Toast.makeText(getApplicationContext(),"此文件为空",Toast.LENGTH_SHORT).show();
156
157
Message msg = Message.obtain();
158
msg.what = 1;
159
mHandler.sendMessage(msg);
160
161
return ;
162
}else {
163
164
currentParents = files ;
165
166
if(currentParents == null || currentParents.length == 0){
167
Log.w(TAG,"此文件为空");
168
// Toast.makeText(getApplicationContext(),"此文件为空",Toast.LENGTH_SHORT).show();
169
Message msg = Message.obtain();
170
msg.what = 1;
171
mHandler.sendMessage(msg);
172
173
return ;
174
175
}
176
177
178
isFile(currentParents);
179
}
180
mHandler.sendEmptyMessage(0);
181
}
182
183
184
}.start();
185
186
}
187
});
188
189
}
190
191
//把文件夹下的文件分好类 文件夹和文件 再分别存入不同的 集合中去
192
private void isFile(File[] currentParents) {
193
int length = currentParents.length;
194
195
directoryList = new ArrayList<String>();
196
matteryList = new ArrayList<String>();
197
198
for (int i = 0 ;i < length ; i++){
199
//判断当前文件是文件夹还是其他
200
boolean directory = currentParents[i].isDirectory();
201
if(directory){
202
directoryList.add(currentParents[i].getName());
203
}else{
204
matteryList.add(currentParents[i].getName());
205
}
206
}
207
}
208
209
class MyBaseAdapter extends BaseAdapter{
210
211
212
public int getCount() {
213
//有目录页就是文件夹的数目和文件的数目加起来
214
return directoryList.size()+matteryList.size();
215
}
216
217
218
public String getItem(int position) {
219
//在条目数在 directoryList.size() 文件夹集合长度前返回文件夹 名字 反之就就是文件
220
if (position < directoryList.size()){
221
return directoryList.get(position);
222
}else{
223
return matteryList.get(position - directoryList.size());
224
}
225
}
226
227
228
public long getItemId(int position) {
229
return position;
230
}
231
232
233
public View getView(int position, View convertView, ViewGroup parent) {
234
View view = null;
235
if(view == null){
236
view = View.inflate(getApplicationContext(),R.layout.file_list,null);
237
}else{
238
view = convertView;
239
}
240
ImageView iv_file_icon = (ImageView)view.findViewById(R.id.iv_file_icon);
241
TextView tv_file_name = (TextView)view.findViewById(R.id.tv_file_name);
242
//不同的数据要区分开来
243
if (position < directoryList.size()){
244
iv_file_icon.setImageResource(R.drawable.folder);
245
tv_file_name.setText((String)getItem(position));
246
}else{
247
iv_file_icon.setImageResource(R.drawable.file);
248
tv_file_name.setText((String)getItem(position));
249
}
250
return view;
251
}
252
}
253
}
254