我尝试使用Storage Access Framework创建和删除目录
public void create() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(DocumentsContract.Document.MIME_TYPE_DIR);
intent.putExtra(Intent.EXTRA_TITLE, "folder");
this.startActivityForResult(intent, 25);
}
public void delete() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
this.startActivityForResult(intent, 27);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData ) {
if (resultCode == Activity.RESULT_OK) {
if (resultData != null) {
Uri uri = resultData.getData();
if (requestCode == 25) {
final int takeFlags = resultData.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
this.getContentResolver().takePersistableUriPermission(uri, takeFlags);
} else if (requestCode == 27) {
deleteFile(uri);
}
}
}
}
private void deleteFile(Uri uri) {
try {
DocumentsContract.deleteDocument(this.getContentResolver(), uri);
} catch (Exception e) {
e.printStackTrace();
}
}
我正确创建了目录,但是无法将其删除,出现错误“无效的URI ...”
谢谢