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.
setFlags
overridesaddFlags
. – SoporUri 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 – HellenistFile 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 puttingIntent intent = Intent.createChooser(target, "Open File");
– HellenistFailed 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<paths> <files-path name="files" path="." /> </paths>
– Kerrill