开源中国 Android 文件夹管理

1. [文件] 我的文件1.0.apk ~ 200KB     下载(54)     

2. [图片] 腾讯手机管家截屏2013071301.png    

3. [文件] MainActivity.java ~ 8KB     下载(26)     

?
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
18
package com.Syl.filemanager;
 
import java.io.File;
import java.util.ArrayList;
import java.util.Currency;
import java.util.HashMap;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
 
@SuppressLint ( "NewApi" )
public class MainActivity extends Activity {
       
     private List<String> paths= null ;  
     private String rootPath= "/" ;  
     private TextView path; 
     private ListView lv;
     private String backPath = "" ;
     private long firstTime = 0 ;
    
     @Override 
     protected void onCreate(Bundle savedInstanceState) {  
         super .onCreate(savedInstanceState);  
                
         /* 设定Layout  activity_main.xml*/       
         if (isSDAvailable ()) {
             setContentView(R.layout.activity_main);  
             path = (TextView)findViewById(R.id.filePath);
             lv = (ListView)findViewById(R.id.list);
             backPath = getSDPath();
             
             rootPath = getSDPath();
             getFileDir(rootPath);
         } else {
             setContentView(R.layout.no_sd); 
         }
                  
     }  
    
     private void getFileDir(String filePath) {     
         path.setText(filePath); 
         
         backPath = filePath;       
         System.out.println( "filePath:" +filePath);
         
         paths = new ArrayList<String>();    
         File f = new File(filePath);    
         File[] files=f.listFiles();
                 
         if (files == null ) {
             return ;
         }
                         
         ArrayList<HashMap<String, Object>>  file_list = new ArrayList<HashMap<String,Object>>();
         if (!filePath.equals(rootPath))  
         {  
             for ( int i = 0 ; i < 2 ; i++) {
                 if (i == 0 ) {
                     HashMap<String, Object> map = new HashMap<String, Object>();
                     map.put( "list_image" , null );
                     map.put( "list_text" , getString(R.string.back_to) + rootPath);
                     file_list.add(map);
                     paths.add(rootPath);  
                 }
                 else {
                     HashMap<String, Object> map = new HashMap<String, Object>();
                     map.put( "list_image" , null );
                     map.put( "list_text" , getString(R.string.back_to_parent));
                     file_list.add(map);               
                     paths.add(f.getParent());                  
                 }
             }
         }  
         
         //将文件放入ArrayList數组中
         for ( int i = 0 ; i < files.length; i++) {    
             HashMap<String, Object> map = new HashMap<String, Object>();
             File file=files[i];
             if (file.canRead()) {
                 if (file.isDirectory()) {
                     map.put( "list_image" , R.drawable.fileicon);
                 } else {
                     //不同文件类型设置不同icon
                     String end=file.getName().substring(file.getName().lastIndexOf( "." )
                             + 1 ,file.getName().length()).toLowerCase();
                     if (end.equals( "jpg" )||end.equals( "gif" )||end.equals( "png" )||  
                               end.equals( "jpeg" )||end.equals( "bmp" )) {
                         map.put( "list_image" , R.drawable.pic);
                     } else if (end.equals( "mp3" )||end.equals( "mid" )||end.equals( "wav" )) {
                         map.put( "list_image" , R.drawable.myfiles_file_mp3_b);
                     } else if (end.equals( "3gp" )||end.equals( "mp4" )||end.equals( "rmvb" )) {
                         map.put( "list_image" , R.drawable.myfiles_icon_video);
                     } else if (end.equals( "apk" )) {
                         map.put( "list_image" , R.drawable.myfiles_file_apk_b);
                     } else if (end.equals( "txt" )||end.equals( "c" )||end.equals( "java" )||end.equals( "xml" )||
                               end.equals( "cpp" )) {
                         map.put( "list_image" , R.drawable.myfiles_file_txt);
                     } else {
                         map.put( "list_image" , R.drawable.other_file);
                     }
                 }
             } else {
                 map.put( "list_image" , R.drawable.other_file);
             }
             map.put( "list_text" , file.getName());
             file_list.add(map);
             paths.add(file.getPath());
         }
         SimpleAdapter listItemAdapter = new SimpleAdapter( this , file_list, R.layout.file_item,
             new String[] { "list_image" , "list_text" },
             new int [] {R.id.imageItem,R.id.textItem});
     
         lv.setAdapter(listItemAdapter);
         lv.setOnItemClickListener( new OnFileManagerItemClickListener());               
     }
         
     @Override
     public boolean onKeyUp( int keyCode, KeyEvent event) {
         // TODO Auto-generated method stub
         if (isSDAvailable ()) {
             long secTime = System.currentTimeMillis();
             System.out.println( "getSDPath():" +getSDPath());
             System.out.println( "backPath:" +backPath);      
             System.out.println( "secTime - firstTime=" +(secTime - firstTime));
             if (KeyEvent.KEYCODE_BACK == keyCode) {
                 if (!backPath.equals(getSDPath())) {
                     getFileDir(( new File(backPath)).getParent());
                 } else {
                     if (secTime - firstTime <= 2500 ) {
                         MainActivity. this .finish();
                         System.exit( 0 );
                     } else {
                         Toast.makeText(MainActivity. this , R.string.double_back, Toast.LENGTH_SHORT).show();
                         firstTime = System.currentTimeMillis();
                         return true ;
                     }
                 }
             }
             return false ;
         } else {
             return super .onKeyUp(keyCode, event);
         }
     }
 
     private class OnFileManagerItemClickListener implements OnItemClickListener {
 
         @Override
         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                 long arg3) {
             // TODO Auto-generated method stub
             File file = new File(paths.get(arg2));  
             if (file.canRead()) {  
               if (file.isDirectory()) {
                   getFileDir(paths.get(arg2));  
               } else {
                   openSelectFile(file);
               }  
             }  
             else {  
               /* 弹出AlertDialog提示*/ 
               new AlertDialog.Builder(MainActivity. this )  
                   .setTitle(R.string.friendly_tip)  
                   .setMessage(R.string.file_cant_read)  
                   .setPositiveButton( "OK" , new DialogInterface.OnClickListener() {  
                       public void onClick(DialogInterface dialog, int which) {
                           dialog.dismiss();
                       }  
                     }).show();       
             }  
          }         
     }
     
     private boolean isSDAvailable() {
         if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
             return true ;
         }
         return false ;
     }
     
     private String getSDPath() {
         String path = Environment.getExternalStoragePublicDirectory( "" ).toString();
         if (path == null ) {
             path = "/" ;
         }
         return path;
     }
     
     private void openSelectFile(File f) {  
         Intent intent = new Intent();  
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
         intent.setAction(android.content.Intent.ACTION_VIEW);  
                
         String type = getFileType(f);  
         intent.setDataAndType(Uri.fromFile(f),type);  
         startActivity(intent);   
     }  
 
   /**
    * 判断文件类型  
    **/
     private String getFileType(File f) {   
         String type= "" ;  
         String fName=f.getName();  
         /* 取得扩展名 */ 
         String end=fName.substring(fName.lastIndexOf( "." )+ 1 ,fName.length()).toLowerCase();   
            
         /* 依扩展名的类型决定FileType */ 
         if (end.equals( "mp3" )||end.equals( "mid" )||end.equals( "wav" )||end.equals( "aac" )||
               end.equals( "amr" ))  
         {  
           type = "audio" ;   
         }  
         else if (end.equals( "3gp" )||end.equals( "mp4" )||end.equals( "rmvb" ))  
         {  
           type = "video" ;  
         }  
         else if (end.equals( "jpg" )||end.equals( "gif" )||end.equals( "png" )||  
         end.equals( "jpeg" )||end.equals( "bmp" ))  
         {  
           type = "image" ;  
         }  
         else if (end.equals( "apk" ))   
         {   
           /* android.permission.INSTALL_PACKAGES */   
           type = "application/vnd.android.package-archive" ;   
         }
         else if (end.equals( "txt" )||end.equals( "c" )||end.equals( "java" )||end.equals( "xml" )||
               end.equals( "cpp" )) {
           type = "text" ;
         }
         else 
         {  
           type= "*" ;  
         }  
         /*若无法直接打开文件,就弹出软件列表*/ 
         if (end.equals( "apk" ))   
         {   
         }   
         else   
         {   
           type += "/*" ;    
         }   
         return type;    
    
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值