这里运用Java I/O、ListActivity、Dialog、Bitmap等实现简单文件管理器,可以查看目录文件,修改文件名,删除文件,打开文件。比较简单,直接看代码:
先看布局文件:
layout/main.xml
01 |
<?xml
version="1.0" encoding="utf-8"?> |
02 |
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" |
03 |
android:orientation="vertical" |
04 |
android:layout_width="fill_parent" |
05 |
android:layout_height="fill_parent" |
06 |
> |
07 |
<ListView |
08 |
android:id="@android:id/list" |
09 |
android:layout_width="wrap_content" |
10 |
android:layout_height="wrap_content" |
11 |
/> |
12 |
</LinearLayout> |
文件列表布局:
layout/file.xml
01 |
<?xml
version="1.0" encoding="utf-8"?> |
02 |
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" |
03 |
android:orientation="horizontal" |
04 |
android:layout_width="fill_parent" |
05 |
android:layout_height="fill_parent" |
06 |
> |
07 |
<ImageView |
08 |
android:id="@+id/imageView" |
09 |
android:layout_width="wrap_content" |
10 |
android:layout_height="wrap_content" |
11 |
/> |
12 |
<TextView |
13 |
android:id="@+id/textView" |
14 |
android:layout_width="wrap_content" |
15 |
android:layout_height="wrap_content" |
16 |
android:textSize="14sp"> |
17 |
</TextView> |
18 |
</LinearLayout> |
修改文件名对话框布局文件:
layout/rename_dialog.xml
01 |
<?xml
version="1.0" encoding="utf-8"?> |
02 |
<LinearLayout |
03 |
xmlns:android="http://schemas.android.com/apk/res/android" |
04 |
android:layout_width="match_parent" |
05 |
android:layout_height="match_parent"> |
06 |
<EditText |
07 |
android:id="@+id/editText" |
08 |
android:layout_width="match_parent" |
09 |
android:layout_height="wrap_content" |
10 |
/> |
11 |
</LinearLayout> |
主Activity:
001 |
public class MainActivity extends ListActivity
{ |
002 |
private static final String
ROOT_PATH = "/"; |
003 |
//存储文件名称 |
004 |
private ArrayList<String>
names = null; |
005 |
//存储文件路径 |
006 |
private ArrayList<String>
paths = null; |
007 |
private View
view; |
008 |
private EditText
editText; |
009 |
/**
Called when the activity is first created. */ |
010 |
@Override |
011 |
public void onCreate(Bundle
savedInstanceState) { |
012 |
super.onCreate(savedInstanceState); |
013 |
setContentView(R.layout.main); |
014 |
//显示文件列表 |
015 |
showFileDir(ROOT_PATH); |
016 |
} |
017 |
private void showFileDir(String
path){ |
018 |
names
= new ArrayList<String>(); |
019 |
paths
= new ArrayList<String>(); |
020 |
File
file = new File(path); |
021 |
File[]
files = file.listFiles(); |
022 |
|
023 |
//如果当前目录不是根目录 |
024 |
if (!ROOT_PATH.equals(path)){ |
025 |
names.add("@1"); |
026 |
paths.add(ROOT_PATH); |
027 |
|
028 |
names.add("@2"); |
029 |
paths.add(file.getParent()); |
030 |
} |
031 |
//添加所有文件 |
032 |
for (File
f : files){ |
033 |
names.add(f.getName()); |
034 |
paths.add(f.getPath()); |
035 |
} |
036 |
this.setListAdapter(new MyAdapter(this,names,
paths)); |
037 |
} |
038 |
@Override |
039 |
protected void onListItemClick(ListView
l, View v, int position, long id)
{ |
040 |
String
path = paths.get(position); |
041 |
File
file = new File(path); |
042 |
//
文件存在并可读 |
043 |
if (file.exists()
&& file.canRead()){ |
044 |
if (file.isDirectory()){ |
045 |
//显示子目录及文件 |
046 |
showFileDir(path); |
047 |
} |
048 |
else{ |
049 |
//处理文件 |
050 |
fileHandle(file); |
051 |
} |
052 |
} |
053 |
//没有权限 |
054 |
else{ |
055 |
Resources
res = getResources(); |
056 |
new AlertDialog.Builder(this).setTitle("Message") |
057 |
.setMessage(res.getString(R.string.no_permission)) |
058 |
.setPositiveButton("OK",new OnClickListener()
{ |
059 |
@Override |
060 |
public void onClick(DialogInterface
dialog, int which)
{ |
061 |
|
062 |
} |
063 |
}).show(); |
064 |
} |
065 |
super.onListItemClick(l,
v, position, id); |
066 |
} |
067 |
//对文件进行增删改 |
068 |
private void fileHandle(final File
file){ |
069 |
OnClickListener
listener = new DialogInterface.OnClickListener()
{ |
070 |
@Override |
071 |
public void onClick(DialogInterface
dialog, int which)
{ |
072 |
//
打开文件 |
073 |
if (which
== 0){ |
074 |
openFile(file); |
075 |
} |
076 |
//修改文件名 |
077 |
else if(which
== 1){ |
078 |
LayoutInflater
factory = LayoutInflater.from(MainActivity.this); |
079 |
view
= factory.inflate(R.layout.rename_dialog, null); |
080 |
editText
= (EditText)view.findViewById(R.id.editText); |
081 |
editText.setText(file.getName()); |
082 |
|
083 |
OnClickListener
listener2 = new DialogInterface.OnClickListener()
{ |
084 |
@Override |
085 |
public void onClick(DialogInterface
dialog, int which)
{ |
086 |
//
TODO Auto-generated method stub |
087 |
String
modifyName = editText.getText().toString(); |
088 |
final String
fpath = file.getParentFile().getPath(); |
089 |
final File
newFile = new File(fpath
+ "/" +
modifyName); |
090 |
if (newFile.exists()){ |
091 |
//排除没有修改情况 |
092 |
if (!modifyName.equals(file.getName())){ |
093 |
new AlertDialog.Builder(MainActivity.this) |
094 |
.setTitle("注意!") |
095 |
.setMessage("文件名已存在,是否覆盖?") |
096 |
.setPositiveButton("确定", newDialogInterface.OnClickListener()
{ |
097 |
@Override |
098 |
public void onClick(DialogInterface
dialog, intwhich)
{ |
099 |
if (file.renameTo(newFile)){ |
100 |
showFileDir(fpath); |
101 |
displayToast("重命名成功!"); |
102 |
} |
103 |
else{ |
104 |
displayToast("重命名失败!"); |
105 |
} |
106 |
} |
107 |
}) |
108 |
.setNegativeButton("取消", newDialogInterface.OnClickListener()
{ |
109 |
@Override |
110 |
public void onClick(DialogInterface
dialog, intwhich)
{ |
111 |
|
112 |
} |
113 |
}) |
114 |
.show(); |
115 |
} |
116 |
} |
117 |
else{ |
118 |
if (file.renameTo(newFile)){ |
119 |
showFileDir(fpath); |
120 |
displayToast("重命名成功!"); |
121 |
} |
122 |
else{ |
123 |
displayToast("重命名失败!"); |
124 |
} |
125 |
} |
126 |
} |
127 |
}; |
128 |
AlertDialog
renameDialog = newAlertDialog.Builder(MainActivity.this).create(); |
129 |
renameDialog.setView(view); |
130 |
renameDialog.setButton("确定",
listener2); |
131 |
renameDialog.setButton2("取消", newDialogInterface.OnClickListener()
{ |
132 |
@Override |
133 |
public void onClick(DialogInterface
dialog, int which)
{ |
134 |
//
TODO Auto-generated method stub |
135 |
|
136 |
} |
137 |
}); |
138 |
renameDialog.show(); |
139 |
} |
140 |
//删除文件 |
141 |
else{ |
142 |
new AlertDialog.Builder(MainActivity.this) |
143 |
.setTitle("注意!") |
144 |
.setMessage("确定要删除此文件吗?") |
145 |
.setPositiveButton("确定", new DialogInterface.OnClickListener()
{ |
146 |
@Override |
147 |
public void onClick(DialogInterface
dialog, int which)
{ |
148 |
if(file.delete()){ |
149 |
//更新文件列表 |
150 |
showFileDir(file.getParent()); |
151 |
displayToast("删除成功!"); |
152 |
} |
153 |
else{ |
154 |
displayToast("删除失败!"); |
155 |
} |
156 |
} |
157 |
}) |
158 |
.setNegativeButton("取消", new DialogInterface.OnClickListener()
{ |
159 |
@Override |
160 |
public void onClick(DialogInterface
dialog, int which)
{ |
161 |
|
162 |
} |
163 |
}).show(); |
164 |
} |
165 |
} |
166 |
}; |
167 |
//选择文件时,弹出增删该操作选项对话框 |
168 |
String[]
menu = {"打开文件","重命名","删除文件"}; |
169 |
new AlertDialog.Builder(MainActivity.this) |
170 |
.setTitle("请选择要进行的操作!") |
171 |
.setItems(menu,
listener) |
172 |
.setPositiveButton("取消", new DialogInterface.OnClickListener()
{ |
173 |
@Override |
174 |
public void onClick(DialogInterface
dialog, int which)
{ |
175 |
|
176 |
} |
177 |
}).show(); |
178 |
} |
179 |
//打开文件 |
180 |
private void openFile(File
file){ |
181 |
Intent
intent = new Intent(); |
182 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
183 |
intent.setAction(android.content.Intent.ACTION_VIEW); |
184 |
|
185 |
String
type = getMIMEType(file); |
186 |
intent.setDataAndType(Uri.fromFile(file),
type); |
187 |
startActivity(intent); |
188 |
} |
189 |
//获取文件mimetype |
190 |
private String
getMIMEType(File file){ |
191 |
String
type = ""; |
192 |
String
name = file.getName(); |
193 |
//文件扩展名 |
194 |
String
end = name.substring(name.lastIndexOf(".")
+ 1,
name.length()).toLowerCase(); |
195 |
if (end.equals("m4a")
|| end.equals("mp3")
|| end.equals("wav")){ |
196 |
type
= "audio"; |
197 |
} |
198 |
else if(end.equals("mp4")
|| end.equals("3gp"))
{ |
199 |
type
= "video"; |
200 |
} |
201 |
else if (end.equals("jpg")
|| end.equals("png")
|| end.equals("jpeg")
|| end.equals("bmp")
|| end.equals("gif")){ |
202 |
type
= "image"; |
203 |
} |
204 |
else { |
205 |
//如果无法直接打开,跳出列表由用户选择 |
206 |
type
= "*"; |
207 |
} |
208 |
type
+= "/*"; |
209 |
return type; |
210 |
} |
211 |
private void displayToast(String
message){ |
212 |
Toast.makeText(MainActivity.this,
message, Toast.LENGTH_SHORT).show(); |
213 |
} |
214 |
} |
自定义适配器:
01 |
public class MyAdapter extends BaseAdapter{ |
02 |
private LayoutInflater
inflater; |
03 |
private Bitmap
directory,file; |
04 |
//存储文件名称 |
05 |
private ArrayList<String>
names = null; |
06 |
//存储文件路径 |
07 |
private ArrayList<String>
paths = null; |
08 |
//参数初始化 |
09 |
public MyAdapter(Context
context,ArrayList<String> na,ArrayList<String> pa){ |
10 |
names
= na; |
11 |
paths
= pa; |
12 |
directory
= BitmapFactory.decodeResource(context.getResources(),R.drawable.d); |
13 |
file
= BitmapFactory.decodeResource(context.getResources(),R.drawable.f); |
14 |
//缩小图片 |
15 |
directory
= small(directory,0.16f); |
16 |
file
= small(file,0.1f); |
17 |
inflater
= LayoutInflater.from(context); |
18 |
} |
19 |
@Override |
20 |
public int getCount()
{ |
21 |
//
TODO Auto-generated method stub |
22 |
return names.size(); |
23 |
} |
24 |
25 |
@Override |
26 |
public Object
getItem(int position)
{ |
27 |
//
TODO Auto-generated method stub |
28 |
return names.get(position); |
29 |
} |
30 |
31 |
@Override |
32 |
public long getItemId(int position)
{ |
33 |
//
TODO Auto-generated method stub |
34 |
return position; |
35 |
} |
36 |
37 |
@Override |
38 |
public View
getView(int position,
View convertView, ViewGroup parent) { |
39 |
//
TODO Auto-generated method stub |
40 |
ViewHolder
holder; |
41 |
if (null ==
convertView){ |
42 |
convertView
= inflater.inflate(R.layout.file, null); |
43 |
holder
= new ViewHolder(); |
44 |
holder.text
= (TextView)convertView.findViewById(R.id.textView); |
45 |
holder.image
= (ImageView)convertView.findViewById(R.id.imageView); |
46 |
|
47 |
convertView.setTag(holder); |
48 |
} |
49 |
else { |
50 |
holder
= (ViewHolder)convertView.getTag(); |
51 |
} |
52 |
File
f = new File(paths.get(position).toString()); |
53 |
if (names.get(position).equals("@1")){ |
54 |
holder.text.setText("/"); |
55 |
holder.image.setImageBitmap(directory); |
56 |
} |
57 |
else if (names.get(position).equals("@2")){ |
58 |
holder.text.setText(".."); |
59 |
holder.image.setImageBitmap(directory); |
60 |
} |
61 |
else{ |
62 |
holder.text.setText(f.getName()); |
63 |
if (f.isDirectory()){ |
64 |
holder.image.setImageBitmap(directory); |
65 |
} |
66 |
else if (f.isFile()){ |
67 |
holder.image.setImageBitmap(file); |
68 |
} |
69 |
else{ |
70 |
System.out.println(f.getName()); |
71 |
} |
72 |
} |
73 |
return convertView; |
74 |
} |
75 |
private class ViewHolder{ |
76 |
private TextView
text; |
77 |
private ImageView
image; |
78 |
} |
79 |
private Bitmap
small(Bitmap map,float num){ |
80 |
Matrix
matrix = new Matrix(); |
81 |
matrix.postScale(num,
num); |
82 |
return Bitmap.createBitmap(map,0,0,map.getWidth(),map.getHeight(),matrix,true); |
83 |
} |
84 |
} |
因为要对文件进行操作,所以在描述文件中授权:
01 |
<?xml
version="1.0" encoding="utf-8"?> |
02 |
<manifest
xmlns:android="http://schemas.android.com/apk/res/android" |
03 |
package="com.test.filemanager" |
04 |
android:versionCode="1" |
05 |
android:versionName="1.0"> |
06 |
<uses-sdk
android:minSdkVersion="10" /> |
07 |
<strong>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> |
08 |
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/></strong>
<application android:icon="@drawable/icon"android:label="@string/app_name"> |
09 |
<activity
android:name=".MainActivity" |
10 |
android:label="@string/app_name"> |
11 |
<intent-filter> |
12 |
<action
android:name="android.intent.action.MAIN" /> |
13 |
<category
android:name="android.intent.category.LAUNCHER" /> |
14 |
</intent-filter> |
15 |
</activity> |
16 |
17 |
</application> |
18 |
</manifest> |
运行结果如下:
查看目录文件

文件重命名:

删除文件:

打开文件:

283

被折叠的 条评论
为什么被折叠?



