2017-05-09 46 views
-1

如---- [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

一整天后,我發現了一個替代method.Look在日誌第一

我改變了兩個參數爲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。

+0

返回**所有**文件,不只是列出的文件 –

+0

是的,現在我只能過濾結果中的後綴,獲取我想要的文件 –