0

I'm struggling to update metadata in the mediastore with a DocumentFile/TreeUri. This is how I tried to do it:

boolean canWrite = documentFile.canWrite(); //returns true

Uri mediaUri = MediaStore.getMediaUri(context, documentFile.getUri());

ContentValuesvalues = new ContentValues();
values.put(MediaStore.MediaColumns.DATE_MODIFIED, newLastModified);

boolean success = context.getContentResolver().update(mediaUri, values,null, null) > 0;

It fails and the logcat reads:

W/MediaProvider: Ignoring mutation of date_modified

What am I doing wrong? The DocumentFile is writable, it is received via Intent.ACTION_OPEN_DOCUMENT_TREE and takePersistableUriPermission was called on the folder. I also tried updating the entry with IS_PENDING before and removed after:

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.IS_PENDING, 1);

boolean success = context.getContentResolver().update(mediaUri, values,
        null, null) > 0; //returns false

returns for whatever reason also false, but I see that the file is prefixed with .pendingxxxx, so it seemed to work

ContentValuesvalues = new ContentValues();
values.put(MediaStore.MediaColumns.DATE_MODIFIED, newLastModified);
values.put(MediaStore.MediaColumns.IS_PENDING, 0);

boolean success = context.getContentResolver().update(mediaUri, values,null, null) > 0; //returns false
JayTee
  • 1,114
  • 2
  • 11
  • 18
  • Please give an example of the value of mediaUri.toString(). – blackapps Nov 04 '21 at 10:59
  • 1
    `W/MediaProvider: Ignoring mutation of date_modified` That is a warning that the provider would not do what you wanted. No error. You could try with enabling permission MANAGE_EXTERNAL_STORAGE. – blackapps Nov 04 '21 at 11:02
  • Have you ever tried to update the meta from a file you inser()ted yourself? Please try. – blackapps Nov 04 '21 at 11:05
  • Did you read? : https://stackoverflow.com/questions/60366394/cant-update-mediastore-on-android-10 – blackapps Nov 04 '21 at 11:06
  • 1
    Search for 'Ignoring mutation of' in: https://android.googlesource.com/platform/packages/providers/MediaProvider/+/master/src/com/android/providers/media/MediaProvider.java There is MANAGE_EXTERNAL_STORAGE too. – blackapps Nov 04 '21 at 11:10
  • Indeed it works when using MANAGE_EXTERNAL_STORAGE. Google didn't accept by application to use it though. Said I should use the mediastore and SAF API in my app instead. Great. Yes I read the oder SO link before. My app has already write access on that file though and no exception is thrown either. – JayTee Nov 04 '21 at 11:20
  • Okay, here is the issue: https://android.googlesource.com/platform/packages/providers/MediaProvider/+/master/src/com/android/providers/media/MediaProvider.java#6361 // Column values controlled by media scanner aren't writable by // apps, since any edits here don't reflect the metadata on // disk, and they'd be overwritten during a rescan. – JayTee Nov 04 '21 at 11:29
  • If edited than metadata on disk should be adapted too of course. – blackapps Nov 04 '21 at 14:03
  • Yeah, but the way to go should be editing the file modified date first, then run the media scanner on that file to update the mediastore data. Raw file access needs MANAGE_EXTERNAL_STORAGE permissions with the now enforced Android 11 API in the playstore. Google deniged me using that permission and suggested me to use the media store api. but obviously that does not work. – JayTee Nov 04 '21 at 18:29

1 Answers1

1

Indexed value of File#lastModified() extracted from this media item. This constant represents a column name that can be used with a ContentProvider through a ContentValues or Cursor object. The values stored in this column are Cursor#FIELD_TYPE_INTEGER , and are read-only and cannot be mutated.

doc

you can't update this,just use File.setLastModified

fansan
  • 73
  • 7