1

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 ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Hardik Kubavat
  • 251
  • 3
  • 23
  • Copy error message -> paste in Google -> notice the **first** result is another question on SO -> sigh -> downvote. I won't even add the link to the dupe. – 2Dee May 25 '18 at 10:00
  • 1
    Possible duplicate of [FileProvider - IllegalArgumentException: Failed to find configured root](https://stackoverflow.com/questions/42516126/fileprovider-illegalargumentexception-failed-to-find-configured-root) – CodeChimp May 25 '18 at 10:05
  • @2Dee Wrong conclusion. Please undo downvote. – greenapps May 25 '18 at 10:10

1 Answers1

1

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);
greenapps
  • 11,154
  • 2
  • 16
  • 19