我正在开发一个项目,其中包含一个用于扫描pdf的模块,来自SD卡的doc xls文件以及它们的列表。我还想制作文件夹列表。
因为我对android很新。任何人都有实现这一目标的想法。
这是我的代码:
public class MediaScannerWrapper implements
MediaScannerConnection.MediaScannerConnectionClient {
private MediaScannerConnection mConnection;
private String mPath;
private String mMimeType;
// filePath - where to scan;
// mime type of media to scan i.e. "image/jpeg".
// use "*/*" for any media
public MediaScannerWrapper(Context ctx, String filePath, String mime){
mPath = filePath;
mMimeType = mime;
mConnection = new MediaScannerConnection(ctx, this);
}
// do the scanning
public void scan() {
mConnection.connect();
}
// start the scan when scanner is ready
public void onMediaScannerConnected() {
mConnection.scanFile(mPath, mMimeType);
Log.w("MediaScannerWrapper", "media file scanned: " + mPath);
}
public void onScanCompleted(String path, Uri uri) {
// when scan is completes, update media file tags
}
}
答案 0 :(得分:23)
public void walkdir(File dir) {
String pdfPattern = ".pdf";
File[] listFile = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
if (listFile[i].getName().endsWith(pdfPattern)){
//Do what ever u want
}
}
}
} }
要搜索整个SD卡,请使用walkdir(Environment.getExternalStorageDirectory());
答案 1 :(得分:2)
使用getExternalStorageDirectory ()获取SD卡路径。 (不要硬编码)
循环浏览每个子文件夹,并使用所需的扩展名检查文件名称。使用字符串endswith()方法检查文件名是否以扩展名结尾。
Here's a sample code可能会对您有所帮助。
答案 2 :(得分:2)
我建议使用commons.io
库处理符号链接和扩展名解析。
扫描扩展名为的文件的任务:
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class ScanTask extends AsyncTask<String,Void,ScanTask.Result> {
@Override
protected Result doInBackground(String... extensions) {
if(extensions == null){
extensions = new String[0];
}
List<File> files = new ArrayList<File>();
boolean success = false;
String status = "";
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
try {
files.addAll(FileUtils.listFiles(Environment.getExternalStorageDirectory(), extensions, true));
success = true;
status = "ok";
} catch (Exception e) {
Log.e("MyApp","File error:",e);
success = false;
status = "Scan error";
}
}else {
success = false;
status = "External storage not available";
}
return new Result(success,status,files);
}
public static class Result{
public final boolean success;
public final String message;
public final List<File> files;
public Result(boolean success, String message,List<File> files) {
this.success = success;
this.message = message;
this.files = files;
}
}
}
用法:
ScanTask task = new ScanTask(){
@Override
protected void onPostExecute(Result result) {
super.onPostExecute(result);
if(result.success){
List<File> files = result.files;
//--do something with files--
}else {
String error = result.message;
//--do something with error message--
}
}
};
task.execute("mp3","jpeg");
答案 3 :(得分:0)
您可以使用此代码:
这是link。
/**
* Class to filter files which are having .mp3 extension
* */
//you can choose the filter for me i put .mp3
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".pdf" ) || name.endsWith(".docs" )) || name.endsWith(".3gp" ) ;
}
}