如---- [doc.docx.xls.xlsx.ppt.pptx.pdf],就像下面的代码一样。但查询结果只有pdf.doc.xls,而不是其他,我很困惑。我发现很多答案都不是我想要的
private void doSearch() {
String[] projection = new String[]{
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.TITLE,
MediaStore.Files.FileColumns.MIME_TYPE};
String selection = MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
+ " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
+ " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
+ " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
+ " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
+ " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
+ " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? ";
String[] selectionArgs = new String[]{
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.ms-powerpoint",
"application/msword",
"application/pdf",
"application/vnd.ms-excel"};
Cursor cursor = getContentResolver().query(MediaStore.Files.getContentUri("external"),
projection,
selection,
selectionArgs,
MediaStore.Files.FileColumns.DATE_MODIFIED + " desc");
String fileName;
String filePath;
while (cursor.moveToNext()) {
String type = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE));
fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.TITLE));
filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));
Log.e("ryze_text", fileName + " -- " + type + "--" + filePath);//Here is the result
}
cursor.close();
cursor = null;
}
答案 0 :(得分:0)
过了一天,我找到了另一种方法。先看看日志
我将两个参数更改为null以指示完整搜索
getContentResolver().query(MediaStore.Files.getContentUri("external"),
projection,
// selection,
// selectionArgs,
null,null,
MediaStore.Files.FileColumns.DATE_MODIFIED + " desc");
Log.e("ryze_text", fileName + " -- " + type + "--" + filePath);
使用上面的代码,我得到了以下日志
20160423.xlsx -- null--/storage/emulated/0/tencent/MicroMsg/Download/20160423.xlsx
我不知道为什么mimeType为null,但我已经可以得到我想要的pptx,xlsx,docx。