FileProvider.getUriForFile() throwing illegal argument exception (Internal Storage issue)

468 Views Asked by At

I believe I have tried almost everything which I have come across but can't get a handle on this error, can someone help please.

My file exists in the folder:

This is how my internal storage looks like EDITED: UPDATED SCREENSHOT Internal storage shows that file does exist

My code is as follows:

val intent = Intent(Intent.ACTION_VIEW)
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                val uriNew = FileProvider.getUriForFile(this@, "com.thatapp.fileprovider", filePDF)
                intent.setDataAndType(uriNew,"application/pdf")
                try{
                    startActivity(intent);
                } catch(e:ActivityNotFoundException){
                    val toast =  Toast.makeText(this,"No app found to open the PDF. Please install Adobe Reader",Toast.LENGTH_SHORT)
                    toast.show()
                }

and the error is as follows:

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.thatapp.sitereport/app_Site_20180811_112642/Report_20180811_122857.pdf

and finally my file_paths look like this:

    <?xml version="1.0" encoding="utf-8"?>

<paths xmlns:android="http://schemas.android.com/apk/res/android">

    <external-path name="My_Images" path="." />
    <files-path
        name="Files"
        path="/"/>
    <!--external-path name="external_files" path="."/-->

</paths>
1

There are 1 best solutions below

1
CommonsWare On BEST ANSWER

Stop putting files and directories directly in /data/data/com.thatapp.sitereport/. I am not certain how you are doing that. Use getFilesDir() on a Context and put your files and subdirectories in there. That would equate to /data/data/com.thatapp.sitereport/files/ (though the exact path will vary by device and user).

At that point, your existing FileProvider setup should work.

If you insist on keeping your files in that location, that's your decision, but then you cannot use FileProvider, as it does not support serving from there. You would need to create your own ContentProvider to serve those files.