未在android 11中使用MediaStore删除的文件,createDeleteRequest()

ws51t4hk  于 2022-12-21  发布在  Android
关注(0)|答案(1)|浏览(178)

我正在尝试删除我在重新安装应用之前创建的录音。我正在使用MediaStore. createDeleteRequest(),它成功地向我显示了一个对话框,询问是否允许删除文件,但当我单击"允许"时,它没有删除文件。
我的录音存储在**"storage/emulated/0/MUSIC/Wear Voice Recorder/"**中
这是我的代码:

public void onClick(View v) {
    List<Uri> uris = new ArrayList<>();
    for (Recordings rec : selectionList) {
        String date = rec.getRecordingDate();
        SimpleDateFormat original = new SimpleDateFormat("d MMM yy, hh:mm:ss a");
        SimpleDateFormat target = new SimpleDateFormat("yyyyMMdd_HHmmss");
        try {
            tempDate = original.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String fileName = rec.getRecordingName() + "_W_" + target.format(tempDate) + ".mp3";

        File directory = Environment.getExternalStorageDirectory();
        file = new File(directory + File.separator + Environment.DIRECTORY_MUSIC + File.separator + "Wear Voice Recorder");
        File[] list = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".mp3");
            }
        });

        for (File mediaFile : list) {
            if (mediaFile.getName().equals(fileName)) {
                arrList.remove(rec);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

                  long mediaID = getFilePathToMediaID(mediaFile.getPath(), RecordingsListActivity.this);
                  Uri Uri_one =ContentUris.withAppendedId(MediaStore.Audio.Media.getContentUri("internal"), mediaID);
                  uris.add(Uri_one);

                }
                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
                    try {
                        mediaFile.delete();
                    } catch (Exception e) {
                        Toast.makeText(RecordingsListActivity.this, "Recording Not Found", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
       requestDeletePermission(RecordingsListActivity.this, uris);
        System.out.println(uris+"");
    }
    adapter.notifyDataSetChanged();
    endSelectionMode();
}
@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();
    }
}
private long getFilePathToMediaID(String path, Context context) {
    long id = 0;
    ContentResolver cr = context.getContentResolver();

    Uri uri = MediaStore.Files.getContentUri("internal");
    String selection = MediaStore.Audio.Media.DATA;
    String[] selectionArgs = {path};
    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;

}

@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(RecordingsListActivity.this, "Deleted successfully!", Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(RecordingsListActivity.this, "Failed to delete!", Toast.LENGTH_SHORT).show();
            }
            break;

    }
}

我真的不太了解MediaStore,这是我的第一个应用程序,它是如此令人沮丧,要求允许删除文件,我的应用程序创建之前,我卸载和重新安装。
我认为URI有问题,当我打印不同文件的URI时,URI是相同的。
它确实向我显示了删除文件的对话框,它还显示了一个祝酒词,上面写着"删除成功!"但文件仍然存在。

cwtwac6a

cwtwac6a1#

URI URI =媒体存储.文件. getContentURI("内部");
试试看:

Uri uri = MediaStore.Video.Media.getContentUri("internal");

但是也许你也应该把"内部"改成MediaStore. VOLUME_EXTERNAL。

相关问题