android应用 获取本地指定类型文件 的两种最优方法
来源:互联网 发布:ai做淘宝照片 编辑:程序博客网 时间:2024/04/29 19:26
刚因为项目有需求,需求移动应用获取本地文件有下面两个
第一个是指定要搜索的目录,第二个是文件类型,譬如“*.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>();@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"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- android应用 获取本地指定类型文件 的两种最优方法
- android应用 获取本地指定类型文件 的两种最优方法
- android应用 获取本地指定类型文件 的两种最优方法
- android--由文件名获取文件Id的两种方法
- android获取文件getMimeType的两种方法
- Android 开发-获取SD卡所有指定类型的文件
- 两种指定函数模板类型的方法
- 利用JMF获取本地摄像头的两种方法
- 获取文件目录的两种方法
- 获取文件目录的两种方法
- 两种获取文件路径的方法
- Android高效获取指定类型文件,获取文件不求人!
- android 获取Bitmap 的两种方法
- android 获取Bitmap 的两种方法
- android获取屏幕分辨率的两种方法
- android 调用应用的两种方法
- Android 应用退出的两种方法
- 获取指定目录下的指定类型的文件
- 两道腾讯面试题
- 重学C++ 函数指针
- 关于curl命令行下载页面为空的问题
- 友元函数
- NYOJ - 阶乘因式分解(二)
- android应用 获取本地指定类型文件 的两种最优方法
- jQuery获取浏览器中的分辨率
- java中取出表单的字段名
- jQuery动态创建节点元素
- 对于我自己来说,看Matlab程序的问题
- Android “再按一次返回键退出程序”实现介绍
- android_加载外部资产文件的几种方案
- CMD常用命令
- Android 开发常用代码片段