PDF opens and closes immediately

1k Views Asked by At

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.

1

There are 1 best solutions below

1
On

Try with:

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