android應用 獲取本地指定型別檔案 的兩種最優方法
阿新 • • 發佈:2018-11-16
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
剛因為專案有需求,需求移動應用獲取本地檔案有下面兩個
第一個是指定要搜尋的目錄,第二個是檔案型別,譬如“*.jpg;*.png;*.gif”.
從資料中查詢得到有多種方法,主要有兩一種,一種是直接查詢,另一種方式是利用廣播的 方式。
- 廣播的方式
通過主動的方式通知系統我們需要檔案列表,要向系統傳送廣播
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(“file://”+ Environment.getExternalStorageDirectory())));
然後通過接收器獲取系統文列表
public classMediaScannerReceiver 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>();@Overrideprotected void onPostExecute(String result) {onFileListCallback.SearchFileListInfo(list);} @Overrideprotected 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);}}} elsescanSDCard(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
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>