2011-03-08 74 views
0

我有一個小內容提供商在我的應用程序包中使用外部應用程序打開一個簡單的pdf,但是當open()運行parcelfiledescription時,會返回一個FileNotFoundExceptionAndroid - 內容提供商文件未找到異常

我不明白什麼是給正確的文件路徑包裹描述正確的sintax ...

public ParcelFileDescriptor openFile(Uri uri, String mode) { 
      Log.i("info","eseguo providing"); 
      URI uri1 = URI.create("file:///data/data/package.name/assets/prova.pdf"); 
      File file = new File(uri1); 
      ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);   
      return parcel; 
} 

感謝您的幫助!


我試過這個簡單的代碼:

URI uri1 = URI.create("file:///android_asset/prova.pdf"); 
File file = new File(uri1); 
Log.i("info","file exist: " + file.exists()); 

但它永遠返回假的!

+0

您的文件位於何處? – 2011-03-08 09:58:45

+0

是您項目的assert文件夾中的文件? – ingsaurabh 2011-03-08 10:00:23

+0

是的,它是在資產文件夾 – 2011-03-08 10:10:53

回答

0

從[ParcelFileDescriptor.open方法] [1](粗體重點煤礦)

FileNotFoundException異常拋出FileNotFoundException異常如果給定的文件不存在或不能與所請求的模式打開。

您是否嘗試將MODE更改爲MODE_READ_ONLY並查看該功能是否有效?

[1]:http://developer.android.com/reference/android/os/ParcelFileDescriptor.html#open(java.io.File,int)

+0

是的我試圖用只讀打開,但結果相同 – 2011-03-08 10:33:39

+0

以下是什麼值,假設您的問題的變量:'file.exists()','文件。 canRead()','file.canWrite()'。所有這些表達式都返回「true」嗎? – 2011-03-08 10:37:21

+0

false,false和... false,我認爲資產文件夾不能從內容提供者那裏得到......在我的項目中資產文件夾包含文件,在我的應用程序的另一個活動中,我可以正確打開包含在webview.loadurl的資產文件夾(「file:///android_asset/image.jpg」);但如果我嘗試用File file = new File(「file:///android_asset/prova.pdf」)打開文件;我得到錯誤,虛假和錯誤 – 2011-03-08 10:54:39