2017-04-04 43 views
0

我開始討論導致我在這裏的另一個問題,所以生病從一開始就是這樣。我有一個應用程序,允許用戶爲他們的客戶創建報價。該應用程序爲引用創建一個pdf並專門將其保存到外部存儲,以便其他應用程序可以訪問它。最近似乎我無法在某些應用程序中查看PDF,當從我的應用程序中打開(谷歌pdf查看器,谷歌驅動器查看器顯示一個空白屏幕,而Adobe Acrobat是完美的罰款)。我知道pdf是好的,因爲我可以從文件管理器中查看它,沒有任何問題。它只是從我的應用程序打開它不起作用。FileProvider的問題

我提供的意圖與URI類似content://path/to/external/storage/MyApp/customerName/quote.pdf

我用來提供它作爲file://...但跑進24 SDK推出的暴露URI異常,所以我乾脆改名的文件://內容:/ /和似乎一直工作,直到我發現谷歌pdf查看器應用程序。

現在我假設它與我顯然應該使用的fileProvider類有關。但我似乎無法得到它的工作,沒有其他問題,我發現有關這個問題似乎工作,所以這裏是我現在有:

file_paths.xml(正在嘗試幾個不同的路徑,似乎沒有工作,也有用戶創建多個客戶文件夾,我不能指定他們在XML文件,因爲我不知道他們將被命名,但爲了測試,我扔了一個在那裏只是爲了看看這是這個問題,它仍然無法正常工作)

<?xml version="1.0" encoding="utf-8"?> 
<paths> 
    <external-path name="external_files" path="." /> 
    <external-path name="myApp" path="MyApp/" /> 
    <external-path name="myAppa" path="MyApp/customerFolder/" /> 
    <external-path name="myAppb" path="/" /> 
</paths> 

清單文件(沒有通過的包括一個用來爲什麼我的工具文件提供:在那裏更換線路,我不知道這是否是造成問題或不是,它習慣於照片保存到同一目錄IM試圖查看從PDF文件)

<provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="${applicationId}.provider" 
     android:exported="false" 
     android:grantUriPermissions="true" 
     tools:replace="authorities" 
     > 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/file_paths" 
      tools:replace="android:resource"/> 
    </provider> 

這啓動意圖查看PDF頁面:

String url = "file:///storage/emulated/0/MyApp/customerFolder/quote.pdf"; 
File mFile = new File(url); 
Uri pdfURI = FileProvider.getUriForFile(activity, activity.getApplicationContext().getPackageName() + ".provider", mFile); 

Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
      intent.setData(pdfURI); 
      intent.setType("application/pdf"); 
      intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);       
activity.startActivity(intent); 

這一直給我下面的錯誤 java.lang.IllegalArgumentException: Failed to find configured root that contains /file:/storage/emulated/0/MyApp/customerFolder/quote.pdf包含FileProvider.getUriForFile()方法的行上的 。我檢查了路徑,它是正確的,

或者,這是我曾經擁有的代碼,它適用於Adobe Acrobat,但不適用於Google pdf查看器或谷歌驅動器查看器。如果沒有解決這個,我很想念隨後的一些方法可以讓我忘記fileProvider:

String url = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? "content://" : "file://") + "/storage/emulated/0/MyApp/customerFolder/quote.pdf"; 
Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.parse(url),"application/pdf"); 
activity.startActivity(intent); 

回答

1

我乾脆改名的文件://內容://和一切似乎是工作,直到我發現瞭解谷歌pdf查看器應用程序。

這應該在零設備上工作。 Adobe可能會嘗試修復這樣的輸入錯誤。

String url = "file:///storage/emulated/0/MyApp/customerFolder/quote.pdf"; 
File mFile = new File(url); 

file:///storage/emulated/0/MyApp/customerFolder/quote.pdf不是文件系統路徑。這是一個URL。對於您的實驗,請使用/storage/emulated/0/MyApp/customerFolder/quote.pdf。最終,不要硬編碼路徑。

<external-path name="myApp" path="MyApp/" />元素將覆蓋該特定的文件系統路徑。

+0

我寫出了url,所以我可以看到實際路徑是什麼,我通常使用'Environment.getExternalStorageDirectory()'。但刪除'file://'後只是使用'/ storage/emulated/...'我似乎更近了一步,它創建了一個url://com.com.MyApp.provider/myApp/ customerFolder/quote.pdf「,但沒有任何pdf查看器正在打開它們,它們會給出」無文件接收「或無法打開文件等錯誤。 – JStephen

+0

該文件存在,並使用external_files路徑給出了不同的url,但結果相同。我覺得這跟以前發生的事情很相似。 PDF應用程序現在至少給我錯誤,但Adobe完全不起作用。 – JStephen

+0

@JStephen:很奇怪。通常情況下,如果您錯過了「FLAG_GRANT_READ_URI_PERMISSION」,我會期待這種行爲,但您擁有它。 [這是我的示例應用程序](https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider/V4FileProvider)的作品,雖然我集成了一個修復程序,以幫助改善與編寫不佳的客戶端應用程序的'FileProvider'兼容性。 – CommonsWare

0

結束瞭解​​決它。基於CommonsWare的示例應用程序,我通過將文件複製到內部存儲並使用該內部存儲網址來實現它,但它仍然無法使用該文件的外部存儲網址。這導致我this answer here顯然setType()將清除由setData()設置的數據。然而,事實並非如此使用內部URL的時候,但我想它無論如何,改變了這種

Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
intent.setData(pdfURI); 
intent.setType("application/pdf"); 

這個

Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
intent.setDataAndType(pdfURI,"application/pdf"); 

,突然將其與外部URL工作過。一個非常令人沮喪的不一致性,特別是在我的情況下,因爲我原本是正確的,但將其分解成兩種方法,認爲如果其中一個出錯了,我會通過行號知道。

我的清單保持不變,但我減少了我的路徑文件,只是這個

<paths> 
    <external-path name="external_files" /> 
</paths> 

希望這可以幫助別人。