微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何删除 Android 10 Scoped Storage 中的图像媒体存储条目和文件

如何解决如何删除 Android 10 Scoped Storage 中的图像媒体存储条目和文件

当我尝试删除由我的应用提供/拥有的图像时,它可以轻松删除。但是对于我的应用程序不拥有的图片文件夹中的共享媒体,我通过捕获 RecoverableSecurityException用户显示提示。但即使在允许之后,我也无法删除该特定图像文件

这是我正在使用的代码,请指出我做错了什么。

  • 删除后图像不会出现在图库或我的应用程序中,但它保留在文件管理器中并在手机重新启动后重新出现在图库中(我猜只有 MediaStore 条目被删除
  • 代码适用于 Android 11 设备。
  • startIntentSenderForResult(intentSender,12,null,null); 的结果是 RESULT_OK

用于获取文件:(此代码在我的活动中)

   try {
            String DIRECTORY_NAME = "%Pictures/App_Name%";
            String selection = MediaStore.MediaColumns.RELATIVE_PATH + " like ? ";
            String[] selectionArgs = new String[]{DIRECTORY_NAME};
            ContentResolver contentResolver = this.getContentResolver();
            Cursor cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection,selectionArgs,null);

            while(cursor.movetoNext()){
                long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
                Uri contentUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,id);

                uriArrayList.add(contentUri);

            }
            cursor.close();
        }
        catch (Exception e){
            e.printstacktrace();
        }

用于删除图像文件:(此代码在我的适配器类中)

    try {
    ContentResolver contentResolver = context.getContentResolver();
    cachedUri = f; //f is Uri 
    contentResolver.delete(f,null);
}catch (SecurityException securityException) {

    RecoverableSecurityException recoverableSecurityException;
    if (securityException instanceof RecoverableSecurityException) {
        recoverableSecurityException =
                (RecoverableSecurityException) securityException;
    } else {
        throw new RuntimeException(
                securityException.getMessage(),securityException);
    }
  
    IntentSender intentSender = recoverableSecurityException.getUserAction()
            .getActionIntent().getIntentSender();
    try {
        resultInterface.onResultTaken(intentSender,cachedUri); //Giving a call to the activity that implements the interface

    } catch (Exception e) {
        e.printstacktrace();
    }
}

提示用户(在我的活动中获取 onActivityResult):

public class SomeActivity extends AppCompatActivity implements ResultTakenListener{

protected void onCreate(){ 
...}

@Override
public void onResultTaken(IntentSender intentSender,Uri uri) throws IntentSender.SendIntentException {
    cacheduri = uri;
    startIntentSenderForResult(intentSender,null);
}

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
    super.onActivityResult(requestCode,resultCode,data);
    if(requestCode == 12){
    Log.d("Result Code is-->",String.valueOf(resultCode)); //This gives RESULT_OK
        if(resultCode == RESULT_OK){
            ContentResolver contentResolver = this.getContentResolver();
            contentResolver.delete(cacheduri,null);
    
        }
    }
}}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。