2

We need to convert a file path to a content uri and pass on for subsequent processing to a common component. When we use "Uri.fromFile(file)" it returns a file uri and not a content uri. Please let us know how to get this converted. Appreciate your help.

for (File file : fileBaseFolder.listFiles()) {
    Uri convertedUri = Uri.fromFile(file);//Need to convert this Uri to a Content uri
    .........
}
Krishnan V S
  • 1,124
  • 1
  • 13
  • 31

3 Answers3

3

Use FileProvider to serve your file.

It gives nice uries.

FileProvider.getUriForFile().

You can serve all files except those from removable micro sd card.

blackapps
  • 8,011
  • 2
  • 11
  • 25
1

try MediaScannerConnection.scanFile

for (File file : fileBaseFolder.listFiles()) {
    MediaScannerConnection.scanFile(this, new String[] { file.getAbsolutePath() }, null,
            (path, uri) -> Log.i(TAG, uri.toString()));
}
atyc
  • 1,056
  • 7
  • 7
0

Take a look at the below link for the solution.

https://www.thetopsites.net/article/52460984.shtml

Also, the same question is asked here.

  • Thanks Mahendra. The limitation with this method seems to be that it might not work for generic files, am I right ? – Krishnan V S Aug 19 '20 at 06:46
  • What do you consider to be generic files? And which of the many solutions proposed in that link are you talking about? – blackapps Aug 19 '20 at 10:20