簡體   English   中英

在Android Nougat中打開時顯示空白屏幕的PDF文件

[英]PDF file when opening in Android Nougat is showing blank screen

我正在創建一個PDF文件並將其保存在本地存儲中。 當試圖打開它時,它在除了Android N之外的所有設備中都是完美的。我可以使用FileProvider在Android N中打開PDF文件,但它顯示為空白。

這是我的URI

content://com.products.provider/external_storage_root/Android/data/com.products/in_17052017_170502_1_1001.pdf

這是我的代碼

Uri path;

File pdfFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
                + "Android" + "/" + "data" + "/" + "com.products" + "/" + file);

if (Build.VERSION.SDK_INT >= 24) {
            path = FileProvider.getUriForFile(getActivity(), "com.products.provider", pdfFile);
        } else {
            path = Uri.fromFile(pdfFile);
        }

        // Setting the intent for pdf reader
        Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
        pdfIntent.setDataAndType(path, "application/pdf");
        pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

        try {
            startActivity(pdfIntent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(getActivity(), "Can't read pdf file", Toast.LENGTH_SHORT).show();
        }

問題在於如何在intent上設置標志

pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

相反,試試這個:

pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Grzegorz Matyszczak的解決方案適用於我的案例。 我和Sabya Sachi的設置非常相似。 更具體地說,這一行允許我看到PDF文件(來自FileProvider.getURIForFile調用的URI):

pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

有關grantUriPermissions更多信息, grantUriPermissions 訪問 android:grantUriPermissions部分。

我可以看到你使用了FileProvider for Nougat。 您必須在AndroidManifest.xml中添加FileProvider標記。

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.mydomain.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
</provider>

FileProvider只能為您事先指定的目錄中的文件生成內容URI。要將此文件鏈接到FileProvider,請添加元素作為定義FileProvider的元素的子元素。

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

您必須為每個目錄指定一個子元素,該目錄包含您想要內容URI的文件。 您可以將它們添加到名為res/xml/file_paths.xml的新文件中。 例如,這些XML元素指定兩個目錄:

<paths  xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="my_images" path="images/"/>
    <files-path name="my_docs" path="docs/"/>
</paths>

- >您必須在file_paths.xml設置PDF目錄路徑

有關詳細信息,請參閱

  1. 用MyFileProvider類替換FileProvider類:

path = MyFileProvider.getUriForFile(getActivity(), "com.products.provider", pdfFile);

其中MyFileProvider應該是擴展FileProvider的泛型類:

public class MyFileProvider extends FileProvider {
}
  1. 通過在provider部分中將MyFileProvider類位置設置為android:name屬性來修改AndroidManifest:
android:name=".utils.MyFileProvider"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM