Dante Dante - 1 year ago 380
Android Question

Android 5.0+ New SD Card Access API DocumentFile.renameTo() UnsupportedOperationException

I've been struggling on how to rename a DocumentFile on Lollipop.
I'm sorry I had tried to search everywhere for the solution, but there seems to be lack of the information online about this new SD Card Access API.

Here is what I have:

String EditText = (Alert_EditText.getText().toString()).trim();
Uri uri = ListViewObject_List.get(LastItemPos).getImageUri();
final DocumentFile documentFile = DocumentFile.fromSingleUri(MainClass.this, uri);

It came up with an UnsupportedOperationException:

Process: com.camera.test, PID: 3362
at android.support.v4.provider.SingleDocumentFile.renameTo(SingleDocumentFile.java:105)
at com.camera.test.MainClass$21.onClick(MainClass.java:986)
at android.view.View.performClick(View.java:5242)
at android.widget.TextView.performClick(TextView.java:10530)
at android.view.View$PerformClick.run(View.java:21185)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6872)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

I also have tried this:

String EditText = (Alert_EditText.getText().toString()).trim();
Uri uri = ListViewObject_List.get(LastItemPos).getImageUri();
File file = new File(uri.getPath());
final DocumentFile documentFile = DocumentFile.fromFile(file);

It didn't seem to do anything: no error, nothing happened...


I can delete the DocumentFile just fine with the following:

Uri uri = ListViewObject_List.get(LastItemPos).getImageUri();
final DocumentFile documentFile = DocumentFile.fromSingleUri(MainClass.this, uri);

Can someone please take a look at it and see what I have done wrong?

Thank you very much for your kindness and help.

Update: this uri is


Answer Source

renameTo() is not supported for a DocumentFile created from fromSingleUri().

You can try DocumentContract.renameDocument(getContentResolver(), uri, theNewDisplayName). getContentResolver() is a method on Context to get a ContentResolver. Note that this changes the display name; the interpretation of "display name" is up to the document provider. Also, not all providers support the renaming of documents, and so this may or may not work.

Note that the Storage Access Provider is not an "SD Card Access API". It is a document API. Those documents come from a document provider of the user's choosing, which may or may not involve removable storage.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download