剛因為項目有需求,需求移動應用獲取本地文件有下面兩個
第一個是指定要搜索的目錄,第二個是文件類型,譬如“*.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<String, String, String>() { ArrayList<FileInfo> list = new ArrayList<FileInfo>(); @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<FileInfo> list); } private static void scanSDCard(File file, String ext, ArrayList<FileInfo> 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
更多資料:http://www.yidin.net/discuz
更多相關的資料請到我的博客:www.yidin.net 留言
歡迎各位同學加入 android 技術二群 222392467