How to force a MediaStore update for a file using only its Content Uri
Asked Answered
H

1

12

In Android Q the field MediaStore.Files.FileColumns.DATA has been deprecated, and may be Null or apps have no rights to read it when targeting such OS version, so will be preferable to work using only a file’s content Uri.

Since the MediaScannerConnection only accepts file paths, I found that for Android Q this is no longer an option.

What would be the way to force an automatic MediaStore update/re-scan of a single file, without knowing its real path and using only its Uri? The intention is not to even try to find the real path and rely only in the Uri.

Consider that the Uri to force the update is a media content Uri (not a SAF Uri).
Example: content://media/external/images/media/123

The solution must not be to re-scan the entire storage or un-mount / mount the storage again, as this will have a high performance hit in our workflow and will make it completely unusable.

And because the intention is to use only the Uri, then to avoid forcing a scan of any specific directory of files, which would also have an impact if it contains lots of files, and implies that a real directory path must be resolved from the Uri, which is not an option.

UPDATE:

We have tried with unsuccessful results the ContentResolver.refresh method introduced in Android O, yet this method doesn't do any refresh at all when it comes to a media content Uri in a format such as: content://media/external/images/media/123

final ContentResolver resolver = context.getContentResolver();
resolver.refresh(uri, null, null);
Hate answered 22/3, 2019 at 13:57 Comment(0)
C
3

I'm currently also trying to redesign my app to use just URIs rather than using all of the hacky solutions to convert them to filepaths (like guessing the path based on the uri authority, etc.) Previously, I used MediaScannerConnection.scanFile(...), which worked without a flaw, but as you've probably already tried, this doesn't work with URIs.

I am finding success by manually updating the MediaStore URI with my new data like this:

public void updateMediaStore(final Uri content, final String title) {
    final ContentValues values = new ContentValues();
    values.put(MediaStore.Audio.AudioColumns.TITLE, title);
    // ... the rest of your data
    cr.update(res, values, null, null);
}

Still, it seems like an oversight to not provide a way to rescan a specific file. For example, if this URI comes from somewhere else, such as on an sdcard via SAF, you will have to first search for it in the MediaStore before updating it.

Cahra answered 16/4, 2019 at 18:56 Comment(2)
Thanks, this is my same current solution. Anyway, I placed a feature request and although it has been passed to the developers team, it haven't got an answer yet. The more developers star it, the more chances we may have to get something new in Android Q regarding this problem: issuetracker.google.com/issues/129089149Hate
Me too. It would be better if MediaScannerConnection.scanFile() directly accept Uris. That will solve this nightmare.Indemonstrable

© 2022 - 2024 — McMap. All rights reserved.