Android Question Try to change lastModified of file in ExternalStorage

Alain75

Member
I write a quite simple jpg compressor : it selects one or more pictures from a folder picked by ExternalStorage in order to get read/write permission and for each file :
  1. Copy the selected picture in DirInternal (name = "temp") ,
  2. Get and store the exif informations of the file (GPSLatitudeRef, GPSLatitude, GPSLongitudeRef, GPSLongitude, DateTime and DateTimeDigitized)
  3. Write a new compressed file in DirInternal (name = "new")
  4. Set the exif informations on the new compressed file (can also set "setLastModified" but it's useless)
  5. Copy the compressed file as "xxx-new.jpg" in ExternalStorage folder, original file name being "xxx.jpg"
  6. And finally try to set "setLastModified" on the new file in ExternalStorage folder with ContentResolver
When ContentResolver uses "uri string" delivered by the content chooser and modified to insert the suffix "-new":
B4X:
Sub SetDateUri(fic As String, date As String)
    Dim uri As Uri, cr As ContentResolver, vl As ContentValues
    cr.initialize("")
    uri.Parse(fic)
    vl.Initialize
    vl.PutString("last_modified",date)
    cr.Update(uri,vl,Null,Null)
    'cr.Update(sto.FindFile(sto.Root,fic.SubString(fic.LastIndexOf("%2F")+3)).Native.RunMethod("getUri",Null),vl,Null,Null)
End Sub
app crashed with message in the log saying "requires MANAGE_DOCUMENTS permission or grantUriPermission". But according to my understandings, MANAGE_DOCUMENTS is not allowed to third party apps and I am a bit lost with granturipermission...
ContentResolver log with uri string from picked file:
copy : content://com.android.externalstorage.documents/document/87C5-17E9%3ADCIM%2FCamera%2F0001.jpg -> temp
copy : new -> content://com.android.externalstorage.documents/document/87C5-17E9%3ADCIM%2FCamera%2F0001-new.jpg
java.lang.RuntimeException: java.lang.SecurityException: Permission Denial: writing com.android.externalstorage.ExternalStorageProvider uri content://com.android.externalstorage.documents/document/87C5-17E9%3ADCIM%2FCamera%2F0001-new.jpg from pid=463, uid=10245 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1750)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.SecurityException: Permission Denial: writing com.android.externalstorage.ExternalStorageProvider uri content://com.android.externalstorage.documents/document/87C5-17E9%3ADCIM%2FCamera%2F0001-new.jpg from pid=463, uid=10245 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
    at android.os.Parcel.createExceptionOrNull(Parcel.java:2386)
    at android.os.Parcel.createException(Parcel.java:2370)
    at android.os.Parcel.readException(Parcel.java:2353)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
    at android.content.ContentProviderProxy.update(ContentProviderNative.java:649)
    at android.content.ContentResolver.update(ContentResolver.java:2366)
    at android.content.ContentResolver.update(ContentResolver.java:2328)
    at anywheresoftware.b4a.objects.ContentResolverWrapper.Update(ContentResolverWrapper.java:110)
    at b4a.jpgsizer.main._setdate2(main.java:1209)
    at b4a.jpgsizer.main$ResumableSub_CompressImage.resume(main.java:760)
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1748)
    ... 7 more
java.lang.RuntimeException: java.lang.SecurityException: Permission Denial: writing com.android.externalstorage.ExternalStorageProvider uri content://com.android.externalstorage.documents/document/87C5-17E9%3ADCIM%2FCamera%2F0001-new.jpg from pid=463, uid=10245 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1750)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.SecurityException: Permission Denial: writing com.android.externalstorage.ExternalStorageProvider uri content://com.android.externalstorage.documents/document/87C5-17E9%3ADCIM%2FCamera%2F0001-new.jpg from pid=463, uid=10245 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
    at android.os.Parcel.createExceptionOrNull(Parcel.java:2386)
    at android.os.Parcel.createException(Parcel.java:2370)
    at android.os.Parcel.readException(Parcel.java:2353)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
    at android.content.ContentProviderProxy.update(ContentProviderNative.java:649)
    at android.content.ContentResolver.update(ContentResolver.java:2366)
    at android.content.ContentResolver.update(ContentResolver.java:2328)
    at anywheresoftware.b4a.objects.ContentResolverWrapper.Update(ContentResolverWrapper.java:110)
    at b4a.jpgsizer.main._setdate2(main.java:1209)
    at b4a.jpgsizer.main$ResumableSub_CompressImage.resume(main.java:760)
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1748)
    ... 7 more

When ContentResolver uses uri of the new file obtained with ExternalFile:
B4X:
Sub SetDateUri(fic As String, date As String)
    Dim uri As Uri, cr As ContentResolver, vl As ContentValues
    cr.initialize("")
    'uri.Parse(fic)
    vl.Initialize
    vl.PutString("last_modified",date)
    'cr.Update(uri,vl,Null,Null)
    cr.Update(sto.FindFile(sto.Root,fic.SubString(fic.LastIndexOf("%2F")+3)).Native.RunMethod("getUri",Null),vl,Null,Null)
End Sub
app crashes with "update not supported" even if I got read/write permission on the folder :
ContentResolver Log using uri from ExternalStorage:
copy : content://com.android.externalstorage.documents/document/87C5-17E9%3ADCIM%2FCamera%2F0001.jpg -> temp
copy : new -> content://com.android.externalstorage.documents/document/87C5-17E9%3ADCIM%2FCamera%2F0001-new.jpg
java.lang.RuntimeException: java.lang.UnsupportedOperationException: Update not supported
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1750)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.UnsupportedOperationException: Update not supported
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:174)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
    at android.content.ContentProviderProxy.update(ContentProviderNative.java:649)
    at android.content.ContentResolver.update(ContentResolver.java:2366)
    at android.content.ContentResolver.update(ContentResolver.java:2328)
    at anywheresoftware.b4a.objects.ContentResolverWrapper.Update(ContentResolverWrapper.java:110)
    at b4a.jpgsizer.main._setdateuri(main.java:1188)
    at b4a.jpgsizer.main$ResumableSub_CompressImage.resume(main.java:748)
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1748)
    ... 7 more

Is there a way to set the modification datetime or is it a lost anyway target ?
Thanks!
 

Alain75

Member
Okay, I try to read deeply "intent android developers" and I may have understood that granturipermission could be given when flags of intent is set to 2 (write) or 3 (read+write). Moreover, all the selected files will be treated as well.
B4X:
Sub PICK_Files   
    Dim in As Intent
    in.Initialize("android.intent.action.GET_CONTENT", "")
    in.PutExtra("android.intent.extra.ALLOW_MULTIPLE", True)
    in.SetType("image/*")
    in.Flags = 2 'FLAG_GRANT_WRITE_URI_PERMISSION
    ChooseFiles(in)
End Sub
Sub ChooseFiles(in As Intent)
    Dim ba As JavaObject = Tools.GetBA(Me)
    Dim ion As Object = ba.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    ba.RunMethod("startActivityForResult", Array As Object(ion, in))
    Wait For ion_Event (MethodName As String, Args() As Object)
    If -1=Args(0) Then
        ...

Am I wright ? Do I need to add something in manifest ?
Thanks !
 
Upvote 0
Top