Android: How to convert File Uri to Content Uri

Viewed 508

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
    .........
}
3 Answers

Use FileProvider to serve your file.

It gives nice uries.

FileProvider.getUriForFile().

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

try MediaScannerConnection.scanFile

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