简体   繁体   English

PDF 立即打开和关闭

[英]PDF opens and closes immediately

In my application I am downloading pdf to Download folder and I am trying to open using below code.在我的应用程序中,我将 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);

Provider definition in manifest file清单文件中的提供程序定义

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

provider_paths.xml provider_paths.xml

<paths>
    <files-path name="CardsDecks" path="." />
</paths>

The app is only showing drive pdf viewer and word options to open the file and not the pdf viewer present in cellphone.该应用程序仅显示用于打开文件的驱动器 pdf 查看器和 word 选项,而不显示手机中的 pdf 查看器。

when i click on drive pdf viewer it opens and closes immediately.当我单击驱动器 pdf 查看器时,它会立即打开和关闭。 i have checked file in download folder, file is present there with content present in it.我已经检查了下载文件夹中的文件,文件存在于其中,其中包含内容。

please help on above.请帮助以上。

尝试:

Uri excelPath = FileProvider.getUriForFile(this, getApplicationContext().getPackageName()+ ".provider", newFile);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM