Android 获取本地文件(遍历本地文件)

刚因为项目有需求,需求移动应用获取本地文件有下面两个 

第一个是指定要搜索的目录,第二个是文件类型,譬如“*.jpg;*.png;*.gif”. 

从资料中查询得到有多种方法,主要有两一种,一种是直接查询,另一种方式是利用广播的方式。 

    广播的方式 

通过主动的方式通知系统我们需要文件列表,要向系统发送广播 

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(“file://” 
+ Environment.getExternalStorageDirectory()))); 

然后通过接收器获取系统文列表 

    public class MediaScannerReceiver extends BroadcastReceiver 
    { 
        private final static String TAG = ”MediaScannerReceiver”; 
        @Override 
        public void onReceive(Context context, Intent intent) { 
            String action = intent.getAction(); 
            Uri uri = intent.getData(); 
            String externalStoragePath = Environment.getExternalStorageDirectory().getPath(); 
            if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { 
                // scan internal storage 
                scan(context, MediaProvider.INTERNAL_VOLUME); 
            } else { 
                if (uri.getScheme().equals(“file”)) { 
                    // handle intents related to external storage 
                    String path = uri.getPath(); 
                    if (action.equals(Intent.ACTION_MEDIA_MOUNTED) && 
                            externalStoragePath.equals(path)) { 
                        scan(context, MediaProvider.EXTERNAL_VOLUME); 
                    } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) && 
                            path != null && path.startsWith(externalStoragePath + ”/”)) { 
                        scanFile(context, path); 
                    } 
                } 
            } 
        } 
        private void scan(Context context, String volume) { 
            Bundle args = new Bundle(); 
            args.putString(“volume”, volume); 
            context.startService( 
                    new Intent(context, MediaScannerService.class).putExtras(args)); 
        } 
        private void scanFile(Context context, String path) { 
            Bundle args = new Bundle(); 
            args.putString(“filepath”, path); 
            context.startService( 
                    new Intent(context, MediaScannerService.class).putExtras(args)); 
        } 
    } 

注意部分: 

通过 Intent.ACTION_MEDIA_MOUNTED 进行全扫描 

通过 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE 扫描某个文件 

上述方法是不支持对文件夹的 即:Uri data 必须是 文件的Uri  如果是文件夹的 其不会起作用的 切记! 

    方法二 直接查找 

      这种方法是最原始的方法,通过获取文件目录递归来查询文件 
       正面是主要实现: 

/** 

* 获取指定位置的指定类型的文件 



* @param path 

*            文件夹路径 

* @param type 

*            文件类型(如“*.jpg;*.png;*.gif”) 

* @return 

*/ 

public static void getFileList(String path, String type, 

final OnFileListCallback onFileListCallback) { 



new AsyncTask() { 

ArrayList list = new ArrayList(); 

@Override 

protected void onPostExecute(String result) { 

onFileListCallback.SearchFileListInfo(list); 





@Override 

protected String doInBackground(String… params) { 

// TODO Auto-generated method stub 



String path = params[1].substring(params[1] 

.lastIndexOf(“.”) + 1); 

File file = new File(params[0]); 

scanSDCard(file,path,list); 

return null; 





}.execute(path, type, “”); 





/** 

* 扫描完成后的回调,获取文件列表必须实现 



* @author cola 



*/ 

public interface OnFileListCallback { 

/** 

* 返回查询的文件列表 

* @param list 文件列表 

*/ 

public void SearchFileListInfo(List list); 





private static void scanSDCard(File file, String ext, ArrayList list) { 

if (file.isDirectory()) { 

File[] files = file.listFiles(); 

if (files != null) { 

for (int i = 0; i < files.length; i++) { 

File tmp = files[i]; 

if (tmp.isFile()) { 

String fileName = tmp.getName(); 

String filePath = tmp.getName(); 

if (fileName.indexOf(“.”) >= 0) { 

fileName = fileName.substring(fileName 

.lastIndexOf(“.”) + 1); 

if (ext != null && ext.equalsIgnoreCase(fileName)) { 

AspLog.i(TAG, filePath); 

FileInfo info = new FileInfo(); 

info.fileName = filePath; 

info.filePath = tmp.getAbsolutePath(); 

list.add(info); 





} else 

scanSDCard(tmp, ext, list); 





} else { 

if (file.isFile()) { 

String fileName = file.getName(); 

String filePath = file.getName(); 

if (fileName.indexOf(“.”) >= 0) { 

fileName = fileName 

.substring(fileName.lastIndexOf(“.”) + 1); 

if (ext != null && ext.equalsIgnoreCase(fileName)) { 

AspLog.i(TAG, filePath); 

FileInfo info = new FileInfo(); 

info.fileName = filePath; 

info.filePath = file.getAbsolutePath(); 

list.add(info); 











转 http://www.yidin.net/?p=9493

你可能感兴趣的:(Android)