国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android開發之簡單文件管理器實現方法

Android開發之簡單文件管理器實現方法

2021-05-04 16:13fwwdn Android

這篇文章主要介紹了Android開發之簡單文件管理器實現方法,簡單實現了Android的文件目錄查看,文件重命名,打開,刪除等功能,需要的朋友可以參考下

本文實例講述了android開發之簡單文件管理器實現方法。分享給大家供大家參考,具體如下:

這里運用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
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
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>

運行結果如下:

查看目錄文件

Android開發之簡單文件管理器實現方法

文件重命名:

Android開發之簡單文件管理器實現方法

刪除文件:

Android開發之簡單文件管理器實現方法

打開文件:

Android開發之簡單文件管理器實現方法

希望本文所述對大家android程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久久久一区 | 亚洲乱码国产乱码精品精98午夜 | 欧美一级片在线观看 | 国产成人综合av | 在线观看91视频 | 久久久久久久国产精品视频 | 国产片一区二区三区 | 91羞羞网站 | 色视频在线播放 | 99精品国产一区二区三区 | 亚洲精品视频一区二区三区 | 日韩欧美一区二区免费 | 久久久久国产精品 | 免费www | 久久久精品观看 | 欧美性猛交一区二区三区精品 | 操操操操操操操 | 一级片免费视频 | 久久久www免费人成精品 | 激情欧美日韩一区二区 | 中文字幕成人 | 噜噜噜噜狠狠狠7777视频 | 亚洲成人第一 | 欧美在线一区二区 | 日本a v在线播放 | 亚洲精品久久 | www.久久99| 亚洲欧美日韩精品 | 国产一级片儿 | 国产欧美日本 | 性爽视频 | 中文字幕黄色 | 国产亚洲精品久久久久动 | 精品久久av | 看日韩毛片| 日韩欧美一区二区三区免费观看 | 国产色在线观看 | 在线a视频| 中文字幕一区二区在线观看 | 久久久久久久av | 日韩在线免费 |