7

I want to copy or move files from the internal storage to the sd card. I do this via the Storage Access Framework (SAF) and the DocumentFile class...

Copying is stream based and the DocumentFile does not have a function like the File class to set the last modified date.

I know, that I move/copy files to the sd card, so I know that I create a local file. With this information, is it somehow possible to update the last modified date of the underlying file of the DocumentFile?

It seems like you can't move/copy files from your internel storage to the sd card without losing the last modified date...

Reading - Working

public long lastModified(DocumentFile file, Context context)
{
    long lastModified = 0;
    final Cursor cursor = context.getContentResolver().query(file.getUri(), null, null, null, null);
    try
    {
        if (cursor.moveToFirst())
            lastModified = cursor.getLong(cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_LAST_MODIFIED));
    }
    finally
    {
        cursor.close();
    }

    return lastModified;
}

WRITING - NOT WORKING

public boolean setLastModified(DocumentFile file, Context context, long time)
{
    ContentValues updateValues = new ContentValues();
    updateValues.put(DocumentsContract.Document.COLUMN_LAST_MODIFIED, time);
    int updated = context.getContentResolver().update(file.getUri(), updateValues, null, null);
    return updated == 1;
}

This fails with a java.lang.UnsupportedOperationException: Update not supported exception...

prom85
  • 16,896
  • 17
  • 122
  • 242
  • I'm sorry but I don't have an answer for you, however I'm interested on how to move and copy files using the Storage Access Framework. Can you help me or do you have a link to a tutorial? – MattButtMatt Jun 02 '16 at 19:04
  • The way I do it is via streams. Moving is copying + deleting (renaming only works if the folder did not change). You can check my UNDOCUMENTED library if you want to: https://github.com/MFlisar/StorageManager/blob/master/lib/src/main/java/com/michaelflisar/storagemanager/StorageManager.java. Check the `initDefaultHandlers` there, it shows how to copy/move files with my wrapper classes... – prom85 Jun 03 '16 at 07:12

2 Answers2

-2

Probably you need permission "android.permission.WRITE_USER_DICTIONARY" in manifest

Alex Kucherenko
  • 20,168
  • 2
  • 26
  • 33
-2

Since API >=26 you can use refresh. This should work to update the Documentfile instantly . This works for me:

 context.getContentResolver().refresh(file.getUri(), null, null, null);
Frank
  • 2,738
  • 2
  • 14
  • 19