如何在 Android 11 中征求用户同意
How To Ask For User Consent In Android 11
它在 android 10 中工作得很好,但在 android 11 中不工作 经过一些研究我发现我们必须征求用户同意,所以我看了一些视频,我不明白怎么办,谁能帮帮我!
File file = new File(childItem.getPath());
file.delete();
if (file.exists()) {
try {
file.getCanonicalFile().delete();
if (file.exists()) {
deleteFile(file.getName());
}
} catch (IOException unused) {
unused.printStackTrace();
}
}
对于安卓11;您需要 user-interaction 才能修改或删除外部存储中的任何文件。您需要使用 createDeleteRequest
来删除文件。
您可以按照以下步骤操作:
if (SDK_INT >= Build.VERSION_CODES.R) {
List<Uri> uris = new ArrayList<>();
// Your for loop starts here, if you want to delete multiple files..
// for(File childItem : yourFileList){
long mediaID = getFilePathToMediaID(childItem.getPath(), MainActivity.this);
Uri Uri_one = ContentUris.withAppendedId(MediaStore.Images.Media.getContentUri("external"), mediaID);
uris.add(Uri_one);
// } /*for ends here */
requestDeletePermission(MainActivity.this, uris);
}
现在像这样创建删除请求;
private static final int REQUEST_PERM_DELETE = 444;
@RequiresApi(api = Build.VERSION_CODES.R)
private void requestDeletePermission(Context context, List<Uri> uri_one) {
PendingIntent pi = MediaStore.createDeleteRequest(context.getContentResolver(), uri_one);
try {
startIntentSenderForResult(pi.getIntentSender(), REQUEST_PERM_DELETE, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
现在在您的 onActivityResult
中处理结果;
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_PERM_DELETE:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(MainActivity.this, "Deleted successfully! Refreshing...", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Failed to delete!", Toast.LENGTH_SHORT).show();
}
break;
}
}
public static long getFilePathToMediaID(String songPath, Context context) {
long id = 0;
ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
String selection = MediaStore.Audio.Media.DATA;
String[] selectionArgs = {songPath};
String[] projection = {MediaStore.Audio.Media._ID};
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cursor = cr.query(uri, projection, selection + "=?", selectionArgs, null);
if (cursor != null) {
while (cursor.moveToNext()) {
int idIndex = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
id = Long.parseLong(cursor.getString(idIndex));
}
}
return id;
}
希望对你有所帮助。
它在 android 10 中工作得很好,但在 android 11 中不工作 经过一些研究我发现我们必须征求用户同意,所以我看了一些视频,我不明白怎么办,谁能帮帮我!
File file = new File(childItem.getPath());
file.delete();
if (file.exists()) {
try {
file.getCanonicalFile().delete();
if (file.exists()) {
deleteFile(file.getName());
}
} catch (IOException unused) {
unused.printStackTrace();
}
}
对于安卓11;您需要 user-interaction 才能修改或删除外部存储中的任何文件。您需要使用 createDeleteRequest
来删除文件。
您可以按照以下步骤操作:
if (SDK_INT >= Build.VERSION_CODES.R) {
List<Uri> uris = new ArrayList<>();
// Your for loop starts here, if you want to delete multiple files..
// for(File childItem : yourFileList){
long mediaID = getFilePathToMediaID(childItem.getPath(), MainActivity.this);
Uri Uri_one = ContentUris.withAppendedId(MediaStore.Images.Media.getContentUri("external"), mediaID);
uris.add(Uri_one);
// } /*for ends here */
requestDeletePermission(MainActivity.this, uris);
}
现在像这样创建删除请求;
private static final int REQUEST_PERM_DELETE = 444;
@RequiresApi(api = Build.VERSION_CODES.R)
private void requestDeletePermission(Context context, List<Uri> uri_one) {
PendingIntent pi = MediaStore.createDeleteRequest(context.getContentResolver(), uri_one);
try {
startIntentSenderForResult(pi.getIntentSender(), REQUEST_PERM_DELETE, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
现在在您的 onActivityResult
中处理结果;
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_PERM_DELETE:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(MainActivity.this, "Deleted successfully! Refreshing...", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Failed to delete!", Toast.LENGTH_SHORT).show();
}
break;
}
}
public static long getFilePathToMediaID(String songPath, Context context) {
long id = 0;
ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
String selection = MediaStore.Audio.Media.DATA;
String[] selectionArgs = {songPath};
String[] projection = {MediaStore.Audio.Media._ID};
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cursor = cr.query(uri, projection, selection + "=?", selectionArgs, null);
if (cursor != null) {
while (cursor.moveToNext()) {
int idIndex = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
id = Long.parseLong(cursor.getString(idIndex));
}
}
return id;
}
希望对你有所帮助。