2017-05-26 63 views
0

我在設備的內部存儲器中有一個pdf文件,位置爲「/storage/emulated/0/MyPdf/test.pdf」。使用FileProvider訪問它時,pdf閱讀器是空白填充黑色。 path_file如下FileProvider在Android Nougat中顯示空白

在我的代碼

<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="external_files" path="."/> </paths>

Java代碼片斷是

File pdfFile = new File(Environment.getExternalStorageDirectory() + "/MyPdf/" + "test.pdf"); 
    Uri path = FileProvider.getUriForFile(getContext(),"com.test.pdf.fileprovider", pdfFile); 
    Intent pdfIntent = new Intent(Intent.ACTION_VIEW); 
    pdfIntent.setDataAndType(path, "application/pdf"); 
    pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    try{ 
     startActivity(pdfIntent); 
    }catch(ActivityNotFoundException e){ 
     Toast.makeText(getContext(), "No Application available to view PDF", Toast.LENGTH_SHORT).show(); 
    } 

提供商在我的清單文件

<provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="com.test.pdf.fileprovider" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/provider_paths"/> 
    </provider> 

請幫我解決這個問題

回答

1

替換:

pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

有:

pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

,你不應該需要FLAG_ACTIVITY_CLEAR_TOP,但你絕對需要FLAG_GRANT_READ_URI_PERMISSION

+0

謝謝..現在它工作正常.. :) – Visakh

0

不應該.getUriForFile()的第二個參數必須是您在清單文件中提到的權限?

"com.test.pdf.fileprovider" instead of ""com.visakh.horizon.horizon.fileprovider" 
+0

對不起,它複製代碼時發生錯誤。兩者都一樣。現在我改變了它的問題 – Visakh