1

在我的應用程序中,用戶可以選擇文件並在適當的應用程序中打開它們(使用ACTION_VIEW意圖)。 我需要在將數據提供給其他應用程序之前對數據進行一些處理。所以我正在使用一個流解決方案:我實現了一個ContentProvider,它實現了openTypedAssetFilewriteDataToPipe(此方法填充由openPipeHelper創建的輸出ParcelFileDescriptor)。Android Gallery不支持流式文件

這個作品:我可以打開.pdf文件,.txt文件等流媒體似乎是正確的。 我可以使用3方應用程序打開圖像。 然而,當我打開使用Gallery的圖像,這是行不通的(圖庫顯示黑屏),我也得到了以下異常:

fail to open myfile.jpg 
UriImage(21890): java.io.FileNotFoundException: Not a whole file 

我看看畫廊源(here)和我能看到異常拋出這裏:

 try { 
      if (MIME_TYPE_JPEG.equalsIgnoreCase(mContentType)) { 
       InputStream is = mApplication.getContentResolver() 
         .openInputStream(mUri); 
       mRotation = Exif.getOrientation(is); 
       Utils.closeSilently(is); 
      } 
      **mFileDescriptor = mApplication.getContentResolver() 
        .openFileDescriptor(mUri, "r");** 
      if (jc.isCancelled()) return STATE_INIT; 
      return STATE_DOWNLOADED; 
     } catch (FileNotFoundException e) { 
      Log.w(TAG, "fail to open: " + mUri, e); 
      return STATE_ERROR; 
     } 

然而,在Gallery應用程序一次,如果我選擇「設置爲牆紙」,然後我可以看到我的形象,它是那麼好流。所以當圖庫打開時出現問題。

經過深入瞭解(在ContentResolver代碼等),我不明白爲什麼它的行爲如此。看起來Gallery不支持流文件。是對的嗎 ? 你有什麼想法嗎?

非常感謝。

+0

創建您自己的畫廊然後訪問選擇的圖像,HTTPS:/ /github.com/luminousman/MultipleImagePick – Rohit

回答

-1
  String scheme = mUri.getScheme(); 

      ContentResolver contentResolver = getContentResolver(); 

      // If we are sent file://something or 
      // content://org.openintents.filemanager/mimetype/something... 
      if (scheme.equals("file") 
        || (scheme.equals("content") && fileUri 
          .getEncodedAuthority().equals(
            "org.openintents.filemanager"))) { 

       // Get the path 
       filePath = fileUri.getPath(); 

       // Trim the path if necessary 
       // openintents filemanager returns 
       // content://org.openintents.filemanager/mimetype//mnt/sdcard/xxxx.jpg 
       if (filePath.startsWith("/mimetype/")) { 
        String trimmedFilePath = filePath 
          .substring("/mimetype/".length()); 
        filePath = trimmedFilePath.substring(trimmedFilePath 
          .indexOf("/")); 
       } 
       } else if (scheme.equals("content")) { 

       // If we are given another content:// URI, look it up in the 
       // media provider 

       filePath = getFilePathFromContentUri(fileUri, 
         contentResolver); 

      } else { 

       Log.e("filePath--------->>>>>", filePath + ""); 
      } 

private String getFilePathFromContentUri(Uri selectedVideoUri, 
     ContentResolver contentResolver) { 
    String filePathString; 
    String[] filePathColumn = { MediaColumns.DATA }; 

    Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, 
      null, null, null); 
    cursor.moveToFirst(); 

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
    filePathString = cursor.getString(columnIndex); 
    cursor.close(); 
    return filePathString; 
    } 

,現在使用這個文件路徑..

try { 
     if (MIME_TYPE_JPEG.equalsIgnoreCase(mContentType)) { 
      InputStream is = mApplication.getContentResolver() 
        .openInputStream(filePath); // ** change is here 
      mRotation = Exif.getOrientation(is); 
      Utils.closeSilently(is); 
     } 
     ** mFileDescriptor = mApplication.getContentResolver() 
       .openFileDescriptor(filePath, "r"); ** 
     if (jc.isCancelled()) return STATE_INIT; 
     return STATE_DOWNLOADED; 
    } catch (FileNotFoundException e) { 
     Log.w(TAG, "fail to open: " + mUri, e); 
     return STATE_ERROR; 
    } 

這是我的工作

+0

對不起,但您的代碼根本不回答原始問題。最初的問題是關於將數據從應用程序流式傳輸到另一個應用程序,以便通過將內容流式傳輸的ContentProvider打開文件。 – Vincent

+0

,現在使用這個文件路徑.. 這是我的工作 –