2017-05-26 20 views
0

「/storage/emulated/0/MyPdf/test.pdf」という場所のデバイスの内部ストレージにPDFファイルがあります。 FileProviderでアクセスすると、pdfリーダーは黒色で塗りつぶされます。次のように path_fileは私のコードでFileProviderアンドロイドに空白が表示されています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のコードスニペットは、私のマニフェストファイルに

<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> 

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(); 
    } 

プロバイダである私は、この問題に

答えて

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()の2番目の引数は、マニフェストファイルで述べた権限でなければなりませんか?

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

申し訳ありませんが、コードをコピーしている間に間違いがありました。両方で同じです。私は質問でそれを変更しました。 – Visakh

関連する問題