gpt4 book ai didi

android - 如何在 Android 10 Scoped Storage 中删除图像(Mediastore 条目和文件)

转载 作者:行者123 更新时间:2023-12-04 23:42:30 28 4
gpt4 key购买 nike

当我尝试删除已由我的应用贡献/拥有的图像时,可以轻松删除它。但是对于我的应用程序不拥有的图片文件夹中的共享媒体,我通过捕获 RecoverableSecurityException 向用户显示提示。 .但即使在 之后允许 ,我无法删除该特定图像文件。
这是我正在使用的代码,请指出我做错了什么。

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

  • 获取文件:(此代码在我的 Activity 中)
       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, null, 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, 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();
    }
    }
    提示用户(在我的 Activity 中获取 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, 12, null, 0, 0, 0, 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, null);

    }
    }
    }}

    最佳答案

    我尚未对其进行测试,但我认为您需要在处理低于 Q 的构建时集成自定义行为。

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

    //your existing code that seems to work above Q
    } else {
    String docsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    File file = new File(docsDir, "fileName.jpg");
    if(file.exists());
    file.delete();
    }

    关于android - 如何在 Android 10 Scoped Storage 中删除图像(Mediastore 条目和文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68164496/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com