公司做了个阅读器,需要从手机导入本地书籍,那么问题来了,怎么快速查找手机内存储的文本文档?
废话不多少,直接上代码
public static List<FileInfo> queryFiles(Context pContext, String type, int minSize) {
LogUtil.d("type="+type+"---minSize="+minSize);
List<FileInfo> lFileInfos = new ArrayList<>();
String[] projection = new String[]{MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.SIZE
};
Cursor cursor = pContext.getContentResolver().query(
Uri.parse("content://media/external/file"),
projection,
MediaStore.Files.FileColumns.DATA + " like ?",
new String[]{"%." + type},
null);
if (cursor != null) {
if (cursor.moveToFirst()) {
int idindex = cursor.getColumnIndex(MediaStore.Files.FileColumns._ID);
int dataindex = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
int sizeindex = cursor.getColumnIndex(MediaStore.Files.FileColumns.SIZE);
do {
FileInfo lFileInfo = new FileInfo();
String id = cursor.getString(idindex);
String path = cursor.getString(dataindex);
int size = cursor.getInt(sizeindex);
lFileInfo.setId(id);
lFileInfo.setFilePath(path);
lFileInfo.setSize(size);
int dot = path.lastIndexOf("/");
lFileInfo.setFileName(path.substring(dot + 1));
lFileInfos.add(lFileInfo);
} while (cursor.moveToNext());
}
}
if (cursor != null)
cursor.close();
List<FileInfo> showList = new ArrayList<>();
for (FileInfo lFileInfo : lFileInfos) {
if (lFileInfo.getSize() > minSize) {
LogUtil.i(lFileInfo.getSize() + "---- minSize=" + minSize);
showList.add(lFileInfo);
}
}
return showList;
}