问题描述
我正在尝试制作一个应用程序,该应用程序可以使用 SAF 直接删除 Android/数据中的文件(而不是文件夹),而无需询问用户(按下按钮删除这些文件)。
我在 Android Developer 中搜索了一些信息,发现需要FLAG_SUPPORTS_DELETE
这是我下面的代码
private void requestAccessAndroidData(Activity activity){
try {
Uri uri = Uri.parse("content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata%2Fcom.garena.game.kgtw%2Ffiles");
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI,uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
activity.startActivityForResult(intent,6666);
} catch (Exception e) {
e.printstacktrace();
}
}
@Override
protected void onCreate(Bundle _savedInstanceState) {
super.onCreate(_savedInstanceState);
setContentView(R.layout.skin);
initialize(_savedInstanceState);
initializeLogic();
AlertDialog.Builder builder = new AlertDialog.Builder(SkinActivity.this);
builder.setTitle("Hint")
.setMessage("Request permission of Android/data")
.setCancelable(false)
.setPositiveButton("Press me to ",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int id) {
Uri uri = Uri.parse("content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata%2Fcom.garena.game.kgtw%2Ffiles");
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI,uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
startActivityForResult(intent,6666);
}
}); AlertDialog alert = builder.create();
alert.show();
}
@RequiresApi(api = Build.VERSION_CODES.N)
private Object[] getDocumentValues(
String[] projection,DocumentFile documentFile) {
Object[] row = new Object[projection.length];
for (int i = 0; i < projection.length; ++i) {
switch (projection[i]) {
case DocumentsContract.Document.COLUMN_FLAGS:
int flag = documentFile.canWrite() ? DocumentsContract.Document.FLAG_DIR_SUPPORTS_CREATE : 0;
flag |= DocumentsContract.Document.FLAG_SUPPORTS_WRITE;
flag |= DocumentsContract.Document.FLAG_SUPPORTS_DELETE;
flag |= DocumentsContract.Document.FLAG_SUPPORTS_REMOVE;
row[i] = flag;
break;
}
}
return row;
}
private void initialize(Bundle _savedInstanceState) {
button1 = (Button) findViewById(R.id.button1);
//Press this to delete files that it must EXIST
button1.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View _view) {
Uri uri = Uri.parse("content://com.android.externalstorage.documents/tree/primary:Android/data/com.garena.game.kgtw/files/Resources/1.40.1/Prefab_Characters/AllActorAction.pkg.bytes");
//the file "AllActorAction.pkg.bytes" is what I want to delete
getContentResolver().delete(uri,null,null);
}
});
但是,当我尝试在 Android 11 上运行时,当我按下此按钮时,应用程序停止工作。
我想弄清楚哪一步是错误的。希望任何有这方面专业知识的人都可以帮助我。 ^_^
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)