android应用 获取本地指定类型文件 的两种最优方法
来源:互联网 发布:淘宝达人怎么找 编辑:程序博客网 时间:2024/05/16 10:09
刚因为项目有需求,需求移动应用获取本地文件有下面两个
第一个是指定要搜索的目录,第二个是文件类型,譬如“*.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);}}}}}
- android应用 获取本地指定类型文件 的两种最优方法
- android应用 获取本地指定类型文件 的两种最优方法
- android应用 获取本地指定类型文件 的两种最优方法
- android--由文件名获取文件Id的两种方法
- android获取文件getMimeType的两种方法
- Android 开发-获取SD卡所有指定类型的文件
- 两种指定函数模板类型的方法
- 利用JMF获取本地摄像头的两种方法
- 获取文件目录的两种方法
- 获取文件目录的两种方法
- 两种获取文件路径的方法
- Android高效获取指定类型文件,获取文件不求人!
- android 获取Bitmap 的两种方法
- android 获取Bitmap 的两种方法
- android获取屏幕分辨率的两种方法
- android 调用应用的两种方法
- Android 应用退出的两种方法
- 获取指定目录下的指定类型的文件
- CentOS 5安装图形界面失败的解决办法
- OTL--c++中连接数据库的方法
- JavaScript真经
- 安装Win7时手动命令创建主分区和逻辑分区
- 常用的 ICU 格式正则表达式
- android应用 获取本地指定类型文件 的两种最优方法
- windows下cvs服务器安装配置
- 外部排序、归并排序、败者树等等。。。
- JavaScript宝座:七大框架论剑
- java自定义注解
- DRIVER_ATTR and DEVICE_ATTR
- Linux下的多进程编程
- vs2010 js/css 代码折叠
- twitter storm安装和storm-start的本地运行