Android 之简单文件管理器
来源:互联网 发布:幸运28软件下载 编辑:程序博客网 时间:2024/06/05 10:23
转自:http://www.open-open.com/lib/view/open1336639193687.html
这里运用Java I/O、ListActivity、Dialog、Bitmap等实现简单文件管理器,可以查看目录文件,修改文件名,删除文件,打开文件。比较简单,直接看代码:
先看布局文件:
layout/main.xml
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:orientation=
"vertical"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
>
<ListView
android:id=
"@android:id/list"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
/>
</LinearLayout>
文件列表布局:
layout/file.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:orientation=
"horizontal"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
>
<ImageView
android:id=
"@+id/imageView"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
/>
<TextView
android:id=
"@+id/textView"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:textSize=
"14sp"
>
</TextView>
</LinearLayout>
修改文件名对话框布局文件:
layout/rename_dialog.xml
1
2
3
4
5
6
7
8
9
10
11
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
>
<EditText
android:id=
"@+id/editText"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
/>
</LinearLayout>
主Activity:
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
public
class
MainActivity
extends
ListActivity {
private
static
final
String ROOT_PATH =
"/"
;
//存储文件名称
private
ArrayList<String> names =
null
;
//存储文件路径
private
ArrayList<String> paths =
null
;
private
View view;
private
EditText editText;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
//显示文件列表
showFileDir(ROOT_PATH);
}
private
void
showFileDir(String path){
names =
new
ArrayList<String>();
paths =
new
ArrayList<String>();
File file =
new
File(path);
File[] files = file.listFiles();
//如果当前目录不是根目录
if
(!ROOT_PATH.equals(path)){
names.add(
"@1"
);
paths.add(ROOT_PATH);
names.add(
"@2"
);
paths.add(file.getParent());
}
//添加所有文件
for
(File f : files){
names.add(f.getName());
paths.add(f.getPath());
}
this
.setListAdapter(
new
MyAdapter(
this
,names, paths));
}
@Override
protected
void
onListItemClick(ListView l, View v,
int
position,
long
id) {
String path = paths.get(position);
File file =
new
File(path);
// 文件存在并可读
if
(file.exists() && file.canRead()){
if
(file.isDirectory()){
//显示子目录及文件
showFileDir(path);
}
else
{
//处理文件
fileHandle(file);
}
}
//没有权限
else
{
Resources res = getResources();
new
AlertDialog.Builder(
this
).setTitle(
"Message"
)
.setMessage(res.getString(R.string.no_permission))
.setPositiveButton(
"OK"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
}
}).show();
}
super
.onListItemClick(l, v, position, id);
}
//对文件进行增删改
private
void
fileHandle(
final
File file){
OnClickListener listener =
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// 打开文件
if
(which ==
0
){
openFile(file);
}
//修改文件名
else
if
(which ==
1
){
LayoutInflater factory = LayoutInflater.from(MainActivity.
this
);
view = factory.inflate(R.layout.rename_dialog,
null
);
editText = (EditText)view.findViewById(R.id.editText);
editText.setText(file.getName());
OnClickListener listener2 =
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
String modifyName = editText.getText().toString();
final
String fpath = file.getParentFile().getPath();
final
File newFile =
new
File(fpath +
"/"
+ modifyName);
if
(newFile.exists()){
//排除没有修改情况
if
(!modifyName.equals(file.getName())){
new
AlertDialog.Builder(MainActivity.
this
)
.setTitle(
"注意!"
)
.setMessage(
"文件名已存在,是否覆盖?"
)
.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
if
(file.renameTo(newFile)){
showFileDir(fpath);
displayToast(
"重命名成功!"
);
}
else
{
displayToast(
"重命名失败!"
);
}
}
})
.setNegativeButton(
"取消"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
}
})
.show();
}
}
else
{
if
(file.renameTo(newFile)){
showFileDir(fpath);
displayToast(
"重命名成功!"
);
}
else
{
displayToast(
"重命名失败!"
);
}
}
}
};
AlertDialog renameDialog =
new
AlertDialog.Builder(MainActivity.
this
).create();
renameDialog.setView(view);
renameDialog.setButton(
"确定"
, listener2);
renameDialog.setButton2(
"取消"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
}
});
renameDialog.show();
}
//删除文件
else
{
new
AlertDialog.Builder(MainActivity.
this
)
.setTitle(
"注意!"
)
.setMessage(
"确定要删除此文件吗?"
)
.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
if
(file.delete()){
//更新文件列表
showFileDir(file.getParent());
displayToast(
"删除成功!"
);
}
else
{
displayToast(
"删除失败!"
);
}
}
})
.setNegativeButton(
"取消"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
}
}).show();
}
}
};
//选择文件时,弹出增删该操作选项对话框
String[] menu = {
"打开文件"
,
"重命名"
,
"删除文件"
};
new
AlertDialog.Builder(MainActivity.
this
)
.setTitle(
"请选择要进行的操作!"
)
.setItems(menu, listener)
.setPositiveButton(
"取消"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
}
}).show();
}
//打开文件
private
void
openFile(File file){
Intent intent =
new
Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
String type = getMIMEType(file);
intent.setDataAndType(Uri.fromFile(file), type);
startActivity(intent);
}
//获取文件mimetype
private
String getMIMEType(File file){
String type =
""
;
String name = file.getName();
//文件扩展名
String end = name.substring(name.lastIndexOf(
"."
) +
1
, name.length()).toLowerCase();
if
(end.equals(
"m4a"
) || end.equals(
"mp3"
) || end.equals(
"wav"
)){
type =
"audio"
;
}
else
if
(end.equals(
"mp4"
) || end.equals(
"3gp"
)) {
type =
"video"
;
}
else
if
(end.equals(
"jpg"
) || end.equals(
"png"
) || end.equals(
"jpeg"
) || end.equals(
"bmp"
) || end.equals(
"gif"
)){
type =
"image"
;
}
else
{
//如果无法直接打开,跳出列表由用户选择
type =
"*"
;
}
type +=
"/*"
;
return
type;
}
private
void
displayToast(String message){
Toast.makeText(MainActivity.
this
, message, Toast.LENGTH_SHORT).show();
}
}
自定义适配器:
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
public
class
MyAdapter
extends
BaseAdapter{
private
LayoutInflater inflater;
private
Bitmap directory,file;
//存储文件名称
private
ArrayList<String> names =
null
;
//存储文件路径
private
ArrayList<String> paths =
null
;
//参数初始化
public
MyAdapter(Context context,ArrayList<String> na,ArrayList<String> pa){
names = na;
paths = pa;
directory = BitmapFactory.decodeResource(context.getResources(),R.drawable.d);
file = BitmapFactory.decodeResource(context.getResources(),R.drawable.f);
//缩小图片
directory = small(directory,
0
.16f);
file = small(file,
0
.1f);
inflater = LayoutInflater.from(context);
}
@Override
public
int
getCount() {
// TODO Auto-generated method stub
return
names.size();
}
@Override
public
Object getItem(
int
position) {
// TODO Auto-generated method stub
return
names.get(position);
}
@Override
public
long
getItemId(
int
position) {
// TODO Auto-generated method stub
return
position;
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if
(
null
== convertView){
convertView = inflater.inflate(R.layout.file,
null
);
holder =
new
ViewHolder();
holder.text = (TextView)convertView.findViewById(R.id.textView);
holder.image = (ImageView)convertView.findViewById(R.id.imageView);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
File f =
new
File(paths.get(position).toString());
if
(names.get(position).equals(
"@1"
)){
holder.text.setText(
"/"
);
holder.image.setImageBitmap(directory);
}
else
if
(names.get(position).equals(
"@2"
)){
holder.text.setText(
".."
);
holder.image.setImageBitmap(directory);
}
else
{
holder.text.setText(f.getName());
if
(f.isDirectory()){
holder.image.setImageBitmap(directory);
}
else
if
(f.isFile()){
holder.image.setImageBitmap(file);
}
else
{
System.out.println(f.getName());
}
}
return
convertView;
}
private
class
ViewHolder{
private
TextView text;
private
ImageView image;
}
private
Bitmap small(Bitmap map,
float
num){
Matrix matrix =
new
Matrix();
matrix.postScale(num, num);
return
Bitmap.createBitmap(map,
0
,
0
,map.getWidth(),map.getHeight(),matrix,
true
);
}
}
因为要对文件进行操作,所以在描述文件中授权:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
package
=
"com.test.filemanager"
android:versionCode=
"1"
android:versionName=
"1.0"
>
<uses-sdk android:minSdkVersion=
"10"
/>
<strong> <uses-permission android:name=
"android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
/>
<uses-permission android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
/></strong> <application android:icon=
"@drawable/icon"
android:label=
"@string/app_name"
>
<activity android:name=
".MainActivity"
android:label=
"@string/app_name"
>
<intent-filter>
<action android:name=
"android.intent.action.MAIN"
/>
<category android:name=
"android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
运行结果如下:
查看目录文件
文件重命名:
删除文件:
打开文件:
0 0
- Android之简单文件管理器
- Android之简单文件管理器
- Android 之简单文件管理器
- Android 之简单文件管理器
- Android 之简单文件管理器
- Android之File类的学习-简单文件管理器
- Android学习之文件管理器
- android 简单文件管理器的实现
- Android自定义适配器---实现简单文件管理器
- Android 源码分享之小米文件管理器
- 简单的文件管理器
- Android 文件管理器 Android文件管理器源代码
- iOS 之文件管理器
- Android学习-文件管理器
- Android 文件管理器
- android简易文件管理器
- 文件管理器-android源代码
- Android 文件管理器
- SVN开启和关闭
- 关于node-webkit的东西
- Java中OutOfMemoryError(内存溢出)的三种情况及解决办法
- codevs 摆花
- sqlite一般方法的封装
- Android 之简单文件管理器
- 友情提醒:欲开发android5.0以上应用,请全部更新开发工具至最新
- 一台电脑安装多个tomcat
- 最近用的C++模板
- 线程写法
- 【github】project is already on GitHub解决办法
- 愿得一人心,白首不相离!
- 添加故事版
- Python Show-Me-the-Code 第 0008 题 提取HTML正文内容