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.

Ajinkya kadam
  • 542
  • 1
  • 6
  • 16
  • Check whether you're able to resolve the intent. For multiple choices, you may try to create an intent chooser. https://stackoverflow.com/a/17453242/4694013 – Anoop M Maddasseri Jan 01 '20 at 11:45
  • 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()*** – Ajinkya kadam Jan 01 '20 at 11:51
  • `setFlags` overrides `addFlags`. – StenSoft Jan 01 '20 at 11:52
  • 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.pdf*** – Ajinkya kadam Jan 01 '20 at 11:54
  • Hi StenSoft, i have removed setflags```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);``` but i am getting error ***java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Download/CardsDecks.pdf*** – Ajinkya kadam Jan 01 '20 at 11:55
  • 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. – Anoop M Maddasseri Jan 01 '20 at 12:04
  • 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");``` – Ajinkya kadam Jan 01 '20 at 12:04
  • 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. – Ajinkya kadam Jan 01 '20 at 12:09
  • `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). – blackapps Jan 01 '20 at 13:39
  • i have added provider definition above in my post also the paths.xml file details. – Ajinkya kadam Jan 01 '20 at 15:49
  • try to replace your file provider with ` ` – Ashvin solanki Jan 01 '20 at 16:03
  • Hi Ashvin... i changed to ****** 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. – Ajinkya kadam Jan 02 '20 at 12:18

1 Answers1

0

Try with:

Uri excelPath = FileProvider.getUriForFile(this, getApplicationContext().getPackageName()+ ".provider", newFile);
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • 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](/help/how-to-ask). – Community Sep 13 '21 at 11:27