0

I have pdf files in Downloads Folder: Environment.DIRECTORY_DOWNLOADS+File.separator+"MyFolder". Listed the files I need in a Recyclerview. I need to view any selected file in any pdfviewer, using intent.

Created a FileProvider with this xml

 <external-path name="download_files" path="Download/" />

This is the error I get:

java.lang.IllegalArgumentException: Failed to find configured root that contains /Download/MyFolder/My File.pdf

This is how I've created the Uri and the intent:

File file = new File(downloadPath  +File.separator+ fileName); 
Uri fileUri = FileProvider.getUriForFile(context,"my.app.provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(fileUri, mediaType);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

So my question again: Can one view or open a file from the ExternalPublicDirectories such as Downloads, Documents or DCIM?

or must the files be in Android>data>com.my.app>?

Bonifes Maina
  • 33
  • 1
  • 6
  • Why do you need a file provider to access the file you wanna read? Assuming you know the path, You can direclty use that path convert to uri and open it, And i'll say use intentChooser rather then normal intent. – Swapnil Padaya May 22 '23 at 19:14
  • Yes it is very well possible to serve files from these three public directories using FileProvider with VIEW or SEND action. – blackapps May 22 '23 at 19:32
  • `path="Download/"` Change to path=".". – blackapps May 22 '23 at 19:33
  • @SwapnilPadaya, thanks for the comment, but it fails before getting to intent-- so normal Intent vs intentChooser doesn't apply? Converting to uri directly leads to UriExposed error- so FileProvider is still required? It fails at ``` Uri fileUri = FileProvider.getUriForFile(context,"my.app.provider", file);``` Error is that it can't find configured root. – Bonifes Maina May 23 '23 at 16:14
  • Thanks @blackapps. Been toying with these paths to no avail. Thinking of copying the file to internal folder first, I guess that's how apps like WPS work? – Bonifes Maina May 23 '23 at 16:22
  • Blame the devil! 'twas the downloadPath all along:: I'd defined it as `Environment.DIRECTORY_DOWNLOADS)++` .. It should have been: `Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)++` – Bonifes Maina May 23 '23 at 17:10

0 Answers0