PDF opens and closes immediately
Asked Answered
H

1

9

In my application I am downloading pdf to Download folder and I am trying to open using below code.

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

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

when i click on drive pdf viewer it opens and closes immediately. i have checked file in download folder, file is present there with content present in it.

please help on above.

Hellenist answered 1/1, 2020 at 11:4 Comment(12)
Check whether you're able to resolve the intent. For multiple choices, you may try to create an intent chooser. https://mcmap.net/q/52954/-android-open-pdf-fileDecoct
Hi Anoop, in that link the code mentioned not works for AndroidX i am getting this error android.os.FileUriExposedException: file:///storage/emulated/0/CardsDecks.pdf exposed beyond app through Intent.getData()Hellenist
setFlags overrides addFlags.Sopor
for androidx i have wrriten like this ... Uri contentUri = FileProvider.getUriForFile(this, "com.cards.mycards.fileprovider", imagePath); Intent intent = new Intent(Intent.ACTION_VIEW) .setDataAndType(Uri.fromFile(imagePath), "application/pdf") .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(intent); but i am getting error java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Download/CardsDecks.pdfHellenist
Hi StenSoft, i have removed setflagsUri contentUri = FileProvider.getUriForFile(this, "com.cards.mycards.fileprovider", imagePath); Intent intent = new Intent(Intent.ACTION_VIEW) .setDataAndType(Uri.fromFile(imagePath), "application/pdf") .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); but i am getting error java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Download/CardsDecks.pdfHellenist
It's a good practice to provide the necessary informations in the description while asking questions like this such as logs, device details so on.Decoct
if i change first line to File imagePath = new File(this.getFilesDir(), "Download"); File newFile = new File(imagePath, FileName+ ".pdf"); Uri contentUri = FileProvider.getUriForFile(this, "com.cards.mycards.fileprovider", newFile); then i think my app is trying to open pdf and it closes immediately even after putting Intent intent = Intent.createChooser(target, "Open File");Hellenist
Also it is not showing pdf viewer application to open the file it is showing drive and word applications to open pdf but pdf viewer is there in my cellphone and i can see pdf file in download folder with content present in it.Hellenist
Failed to find configured root.... Show the provider definition in manifest file. Also show provider paths .xml file. (In your post both. Not in comments).Transference
i have added provider definition above in my post also the paths.xml file details.Hellenist
try to replace your file provider with <paths> <files-path name="files" path="." /> </paths>Kerrill
Hi Ashvin... i changed to <paths><files-path name="files" path="." /></paths> but its not working. it is showing only 2 options to open pdf drive pdf viewer and word and when i click on drive pdf viewer it tries to open the file and closes immediately. also i am not getting any error in console.Hellenist
C
0

Try with:

Uri excelPath = FileProvider.getUriForFile(this, getApplicationContext().getPackageName()+ ".provider", newFile);
Coverture answered 13/9, 2021 at 11:20 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Larissa

© 2022 - 2024 — McMap. All rights reserved.