PDF 立即打开和关闭

Aji*_*dam 8 java android android-pdf-api androidpdfviewer

在我的应用程序中,我将 pdf 下载到下载文件夹,并尝试使用以下代码打开。

String FileName="CardsDecks";
File imagePath = new File(this.getFilesDir(), "Download");
File newFile = new File(imagePath, FileName+ ".pdf");
Uri contentUri = FileProvider.getUriForFile(this, "com.cards.mycards.fileprovider", newFile);

Intent intent =new Intent(Intent.ACTION_VIEW)                                
    .setDataAndType(contentUri, "application/pdf")
    .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

Intent intent1 = Intent.createChooser(intent, "Open File");

startActivity(intent1);
Run Code Online (Sandbox Code Playgroud)

清单文件中的提供程序定义

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.cards.mycards.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)

provider_paths.xml

<paths>
    <files-path name="CardsDecks" path="." />
</paths>
Run Code Online (Sandbox Code Playgroud)

该应用程序仅显示用于打开文件的驱动器 pdf 查看器和 word 选项,而不显示手机中的 pdf 查看器。

当我单击驱动器 pdf 查看器时,它会立即打开和关闭。我已经检查了下载文件夹中的文件,文件存在于其中,其中包含内容。

请帮助以上。

小智 0

尝试使用:

Uri excelPath = FileProvider.getUriForFile(this, getApplicationContext().getPackageName()+ ".provider", newFile);
Run Code Online (Sandbox Code Playgroud)