Hardik Kubavat
Hardik Kubavat

Reputation: 301

Opening a downloaded PDF file

I'm trying to open downloaded pdf file trough implicit intent using FileProvider.

I'm using DownloadManager for downloading pdf file from remote server, It's working fine. Which is store at it's destination.

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadURL));
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    request.setAllowedOverRoaming(false);
    request.setTitle(mFilename);
    request.setDescription("Downloading...");
    request.setVisibleInDownloadsUi(true);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/FOLDER_NAME/" + mFilename);

After Finishing Download i want to open it.

public void OpenPdfFile(){
    File sharedFile = new File(Environment.DIRECTORY_DOWNLOADS, "/FOLDER_NAME/" + mFilename);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID+ ".provider", sharedFile);
    intent.setDataAndType(uri, "application/pdf");

    PackageManager pm = mContext.getPackageManager();
    if (intent.resolveActivity(pm) != null) {
        mContext.startActivity(intent);
    }
}

in Manifest file

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

and the provider_paths.xml as like

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external-path" path="." />
</paths>

it troughs me this error

java.lang.IllegalArgumentException: Failed to find configured root that contains /Download/FOLDER_NAME/demo_presentationfile.PDF

Any suggestions ?

Upvotes: 1

Views: 564

Answers (1)

greenapps
greenapps

Reputation: 11224

File sharedFile = new File(Environment.DIRECTORY_DOWNLOADS, "/FOLDER_NAME/" + mFilename);

That evaluates to an impossible file system path. Which you would see if you inspected the value of sharedFile.getAbsolutePath().

Change to:

File sharedFile = new File(Environment.getExternalStoragePublicDirectory( 
        Environment.DIRECTORY_DOWNLOADS), "FOLDER_NAME/" + mFilename);

Upvotes: 1

Related Questions