2016-02-05 26 views
1

我正在嘗試獲取epubfile的MIME類型,其中,作爲維基百科上的聽是application/epub+zip。但是,該方法返回null。用於EPUB的getMimeTypeFromExtension返回null

Uri uri = MediaStore.Files.getContentUri("external"); 

String[] projection = null; 
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "=" 
         + MediaStore.Files.FileColumns.MEDIA_TYPE_NONE; 
String[] selectionArgs = null; // there is no ? in selection so null here 
String sortOrder = null; // unordered 
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?"; 
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("epub"); //this line here returns null 
// String mimeType = "application/epub+zip" this returns null too. 
String[] selectionArgsPdf = new String[]{ mimeType }; 
Cursor cur = cr.query(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder); 

是否有其他方法導入epub文件?這是可能的,因爲有些應用可以做到這一點。 (Moon Reader)

+0

[加入的epub星期四12月11日13時07分19秒2014](https://android.googlesource.com/platform/libcore/+log/master/luni/src/main/java/libcore/ net/MimeUtils.java)...所以在此之前的每個Android版本顯然都會返回null ... – Selvin

+0

您可以確認Android版本嗎? –

+0

很明顯,我如何爲舊設備做這項工作? –

回答

0

This works。

Uri uri = MediaStore.Files.getContentUri("external"); 

    // every column, although that is huge waste, you probably need 
    // BaseColumns.DATA (the path) only. 
      String[] projection = null; 

    // exclude media files, they would be here also. 
      String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "=" 
        + MediaStore.Files.FileColumns.MEDIA_TYPE_NONE; 
      String[] selectionArgs = null; // there is no ? in selection so null here 

      Cursor cur = cr.query(uri, projection, selection,selectionArgs,null); 
      int NumberOfPDFS =cur.getCount(); 
      cl(Integer.toString(NumberOfPDFS)); 


      cur.moveToFirst(); 
      while (cur.isAfterLast() == false) { 
       if(cur.getString(1).lastIndexOf(".epub")!=-1){ 
        //do whatever. 
       } 

       cur.moveToNext(); 
      } 
      cur.close(); 

     }