• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

DocumentsContract.copyDocument

germes

Lurker
Nov 17, 2020
1
0
I have a problem with DocumentsContract.copyDocument

Each time my program crashes: "java.lang.UnsupportedOperationException: Copy not supported" The curious thing is the below-listed actions are working fine!!

DocumentsContract.moveDocument

DocumentsContract.deleteDocument

DocumentsContract.renameDocument

DocumentsContract.createDocument

Permission to SD card access is apparently granted (above actions would fail without permission)

How come document moving, deleting and renaming is supported but not copying? Can anyone help me with this issue?

Is there a great conceptual difference between copyDocument and moveDocument. Why move does work and copy does not?

Any hint will be highly appreciated.





package com.example.forum14_11_2020;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.documentfile.provider.DocumentFile;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.UriPermission;
import android.net.Uri;
import android.os.Bundle;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.provider.DocumentsContract;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

public class MainActivity extends AppCompatActivity {

Uri uri;
DocumentFile pickedDir;
Button Button1;

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button1 = new Button(MainActivity.this);
Button1 = findViewById(R.id.button1);
Button1.setOnClickListener(onClickListener_Button1);

takeCardUriPermission("storage/1619-0D07");

}


View.OnClickListener onClickListener_Button1 = new View.OnClickListener() {
@override
public void onClick(View v) {

uri = getUri();
pickedDir = DocumentFile.fromTreeUri(getBaseContext(), uri);

DocumentFile file = pickedDir.findFile("attempt.txt");
DocumentFile folder = pickedDir.findFile("folder");

try {
//DocumentsContract.moveDocument(getContentResolver(), file.getUri(), folder.getUri(), folder.getUri()); // does work!
//DocumentsContract.deleteDocument(getContentResolver(), file.getUri()); // does work!
//DocumentsContract.renameDocument(getContentResolver(), file.getUri(), file.getName() + "_rename"); // does work!
DocumentsContract.copyDocument(getContentResolver(), file.getUri(), folder.getUri()); // does not work, why???

} catch (UnsupportedOperationException | FileNotFoundException e) {
e.printStackTrace();
e.getMessage();
}

}
};





public void takeCardUriPermission(String sdCardRootPath) {

int stop=1;

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
File sdCard = new File(sdCardRootPath);
StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
StorageVolume storageVolume = storageManager.getStorageVolume(sdCard);
Intent intent = storageVolume.createAccessIntent(null);
try {
startActivityForResult(intent, 4010);
} catch (ActivityNotFoundException e) {
}
}
}


protected void onActivityResult(int requestCode, int resultCode, @nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 4010) {

Uri uri = data.getData();

grantUriPermission(getPackageName(), uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
Intent.FLAG_GRANT_READ_URI_PERMISSION);

final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
Intent.FLAG_GRANT_READ_URI_PERMISSION);

getContentResolver().takePersistableUriPermission(uri, takeFlags);
}
}

public Uri getUri() {
List<UriPermission> persistedUriPermissions = getContentResolver().getPersistedUriPermissions();
if (persistedUriPermissions.size() > 0) {
UriPermission uriPermission = persistedUriPermissions.get(0);
return uriPermission.getUri();
}
return null;
}






}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones