2013年8月3日星期六

android applications for specific types of files both locallyoptimal method

 Just because there is a demand

projects , the demand for mobile applications have the following two local files

 

first one is to specify the directory to be searched , and the second is the file type , such as "*. jpg; *. png ; *. gif".

 

queries from the data obtained with a variety of methods , there are two main kind is a direct query , another way is to use the broadcast way .

 
      
  1. broadcasts
  2.  
 

notification system through a proactive approach we need to file list, send a broadcast to your system

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

then access the system through the receiver text list

 
  
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);

}

}

}

}

}
 
 

this address : http://www.yidin.net/?p=9493

 

More information : http://www.yidin.net/discuz

 

more information please go to my blog : www.yidin.net comment

 

welcome students to join android technology two groups 222392467

没有评论:

发表评论