android documentscontract 源码,file - DocumentsContract.copyDocument - Stack Overflow

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

} 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 persistedUriPermissions = getContentResolver().getPersistedUriPermissions();

if (persistedUriPermissions.size() > 0) {

UriPermission uriPermission = persistedUriPermissions.get(0);

return uriPermission.getUri();

}

return null;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值