2017-01-23 68 views
1

提供商ERR_UNKNOWN_URL_SCHEME當負載文本文件中的manifest.xml使用fileprovider

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

資源文件

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

使用fileprovider

Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file); 
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(uri, "*/*"); 
intent.startActivity(intent); 
的10

參考here

說明: 我無法打開圖像,文本文件,使用此文件提供PDF格式。 當我打開文件文本文件,它給我ERR_UNKNOWN_URL_SCHEME。 如果我打開圖片或PDF它沒有顯示任何

+0

請告訴uri.toString()的值。 – greenapps

+0

和file.getAbsolutePath()。 – greenapps

+0

/存儲/模擬/ 0/KOOPSv3Sales/KOOPSv3Sales郵件附件/ 29ca8aed.txt –

回答

0

您需要將「FLAG_GRANT_READ_URI_PERMISSION」標誌添加到您的意圖:

Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file); 
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(uri, "*/*"); 
intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION); 
intent.startActivity(intent); 

https://developer.android.com/reference/android/content/Intent.html#FLAG_GRANT_READ_URI_PERMISSION

FLAG_GRANT_READ_URI_PERMISSION如果設置,這個接收者意向 將被授予對Intent的數據和在其ClipData中指定的任何URI的URI執行讀取操作的權限。當 應用於Intent的ClipData時,所有的URIs以及遞歸的 遍歷數據或者其他ClipData中的Intent項都會被授予 ;只使用頂級意圖的授予標誌。