歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android之簡單文件管理器

Android之簡單文件管理器

日期:2017/3/1 10:23:13   编辑:Linux編程

這裡運用Java I/O、ListActivity、Dialog、Bitmap等實現簡單文件管理器,可以查看目錄文件,修改文件名,刪除文件,打開文件。比較簡單,直接看代碼:

先看布局文件:

layout/main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <ListView
  8. android:id="@android:id/list"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. />
  12. </LinearLayout>
文件列表布局:

layout/file.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <ImageView
  8. android:id="@+id/imageView"
  9. 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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <EditText
  7. android:id="@+id/editText"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. />
  11. </LinearLayout>
主Activity:
  1. public class MainActivity extends ListActivity {
  2. private static final String ROOT_PATH = "/";
  3. //存儲文件名稱
  4. private ArrayList<String> names = null;
  5. //存儲文件路徑
  6. private ArrayList<String> paths = null;
  7. private View view;
  8. private EditText editText;
  9. /** Called when the activity is first created. */
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. //顯示文件列表
  15. showFileDir(ROOT_PATH);
  16. }
  17. private void showFileDir(String path){
  18. names = new ArrayList<String>();
  19. paths = new ArrayList<String>();
  20. File file = new File(path);
  21. File[] files = file.listFiles();
  22. //如果當前目錄不是根目錄
  23. if (!ROOT_PATH.equals(path)){
  24. names.add("@1");
  25. paths.add(ROOT_PATH);
  26. names.add("@2");
  27. paths.add(file.getParent());
  28. }
  29. //添加所有文件
  30. for (File f : files){
  31. names.add(f.getName());
  32. paths.add(f.getPath());
  33. }
  34. this.setListAdapter(new MyAdapter(this,names, paths));
  35. }
  36. @Override
  37. protected void onListItemClick(ListView l, View v, int position, long id) {
  38. String path = paths.get(position);
  39. File file = new File(path);
  40. // 文件存在並可讀
  41. if (file.exists() && file.canRead()){
  42. if (file.isDirectory()){
  43. //顯示子目錄及文件
  44. showFileDir(path);
  45. }
  46. else{
  47. //處理文件
  48. fileHandle(file);
  49. }
  50. }
  51. //沒有權限
  52. else{
  53. Resources res = getResources();
  54. new AlertDialog.Builder(this).setTitle("Message")
  55. .setMessage(res.getString(R.string.no_permission))
  56. .setPositiveButton("OK",new OnClickListener() {
  57. @Override
  58. public void onClick(DialogInterface dialog, int which) {
  59. }
  60. }).show();
  61. }
  62. super.onListItemClick(l, v, position, id);
  63. }
  64. //對文件進行增刪改
  65. private void fileHandle(final File file){
  66. OnClickListener listener = new DialogInterface.OnClickListener() {
  67. @Override
  68. public void onClick(DialogInterface dialog, int which) {
  69. // 打開文件
  70. if (which == 0){
  71. openFile(file);
  72. }
  73. //修改文件名
  74. else if(which == 1){
  75. LayoutInflater factory = LayoutInflater.from(MainActivity.this);
  76. view = factory.inflate(R.layout.rename_dialog, null);
  77. editText = (EditText)view.findViewById(R.id.editText);
  78. editText.setText(file.getName());
  79. OnClickListener listener2 = new DialogInterface.OnClickListener() {
  80. @Override
  81. public void onClick(DialogInterface dialog, int which) {
  82. // TODO Auto-generated method stub
  83. String modifyName = editText.getText().toString();
  84. final String fpath = file.getParentFile().getPath();
  85. final File newFile = new File(fpath + "/" + modifyName);
  86. if (newFile.exists()){
  87. //排除沒有修改情況
  88. if (!modifyName.equals(file.getName())){
  89. new AlertDialog.Builder(MainActivity.this)
  90. .setTitle("注意!")
  91. .setMessage("文件名已存在,是否覆蓋?")
  92. .setPositiveButton("確定", new DialogInterface.OnClickListener() {
  93. @Override
  94. public void onClick(DialogInterface dialog, int which) {
  95. if (file.renameTo(newFile)){
  96. showFileDir(fpath);
  97. displayToast("重命名成功!");
  98. }
  99. else{
  100. displayToast("重命名失敗!");
  101. }
  102. }
  103. })
  104. .setNegativeButton("取消", new DialogInterface.OnClickListener() {
  105. @Override
  106. public void onClick(DialogInterface dialog, int which) {
  107. }
  108. })
  109. .show();
  110. }
  111. }
  112. else{
  113. if (file.renameTo(newFile)){
  114. showFileDir(fpath);
  115. displayToast("重命名成功!");
  116. }
  117. else{
  118. displayToast("重命名失敗!");
  119. }
  120. }
  121. }
  122. };
  123. AlertDialog renameDialog = new AlertDialog.Builder(MainActivity.this).create();
  124. renameDialog.setView(view);
  125. renameDialog.setButton("確定", listener2);
  126. renameDialog.setButton2("取消", new DialogInterface.OnClickListener() {
  127. @Override
  128. public void onClick(DialogInterface dialog, int which) {
  129. // TODO Auto-generated method stub
  130. }
  131. });
  132. renameDialog.show();
  133. }
  134. //刪除文件
  135. else{
  136. new AlertDialog.Builder(MainActivity.this)
  137. .setTitle("注意!")
  138. .setMessage("確定要刪除此文件嗎?")
  139. .setPositiveButton("確定", new DialogInterface.OnClickListener() {
  140. @Override
  141. public void onClick(DialogInterface dialog, int which) {
  142. if(file.delete()){
  143. //更新文件列表
  144. showFileDir(file.getParent());
  145. displayToast("刪除成功!");
  146. }
  147. else{
  148. displayToast("刪除失敗!");
  149. }
  150. }
  151. })
  152. .setNegativeButton("取消", new DialogInterface.OnClickListener() {
  153. @Override
  154. public void onClick(DialogInterface dialog, int which) {
  155. }
  156. }).show();
  157. }
  158. }
  159. };
  160. //選擇文件時,彈出增刪該操作選項對話框
  161. String[] menu = {"打開文件","重命名","刪除文件"};
  162. new AlertDialog.Builder(MainActivity.this)
  163. .setTitle("請選擇要進行的操作!")
  164. .setItems(menu, listener)
  165. .setPositiveButton("取消", new DialogInterface.OnClickListener() {
  166. @Override
  167. public void onClick(DialogInterface dialog, int which) {
  168. }
  169. }).show();
  170. }
  171. //打開文件
  172. private void openFile(File file){
  173. Intent intent = new Intent();
  174. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  175. intent.setAction(android.content.Intent.ACTION_VIEW);
  176. String type = getMIMEType(file);
  177. intent.setDataAndType(Uri.fromFile(file), type);
  178. startActivity(intent);
  179. }
  180. //獲取文件mimetype
  181. private String getMIMEType(File file){
  182. String type = "";
  183. String name = file.getName();
  184. //文件擴展名
  185. String end = name.substring(name.lastIndexOf(".") + 1, name.length()).toLowerCase();
  186. if (end.equals("m4a") || end.equals("mp3") || end.equals("wav")){
  187. type = "audio";
  188. }
  189. else if(end.equals("mp4") || end.equals("3gp")) {
  190. type = "video";
  191. }
  192. else if (end.equals("jpg") || end.equals("png") || end.equals("jpeg") || end.equals("bmp") || end.equals("gif")){
  193. type = "image";
  194. }
  195. else {
  196. //如果無法直接打開,跳出列表由用戶選擇
  197. type = "*";
  198. }
  199. type += "/*";
  200. return type;
  201. }
  202. private void displayToast(String message){
  203. Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
  204. }
  205. }
Copyright © Linux教程網 All Rights Reserved