0

I have a music player app, and it used to work when I tried to delete a file. However, since API 29, I'm getting the following error when I tried to delete the file via the contentResolver.delete.

android.app.RecoverableSecurityException: musicplayer has no access to content://media/external/audio/media/1324

 AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setMessage("Are you sure you want to delete " + mSongList.get(selectedPosition).getSongName());
alert.setPositiveButton("YES", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ContentResolver contentResolver = getActivity().getContentResolver();
                                    contentResolver.delete(mSongList.get(selectedPosition).getSongUri(), null, null);
        mSongList.remove(selectedPosition);
        mAdapterListFile.notifyDataSetChanged();
        serviceMusic.setSongList(mSongList);
            dialog.dismiss();
        }
    });
    alert.setNegativeButton("NO", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    }
                });

    alert.show();
Julia
  • 1,207
  • 4
  • 29
  • 47
  • looks related to: https://stackoverflow.com/a/59736650/5482166 – davehenry Dec 18 '20 at 16:12
  • 1
    yah, but I can do it with other people's app. Using their app, I was able to delete the file. Is there any alternative to my delete method in the new API? – Julia Dec 18 '20 at 16:17

1 Answers1

1

You should take recoverableSecurityException.userAction.actionIntent.intentSender from exception and using Activity.startIntentSenderForResult() aks the permission to delete the file.
Check this sample project: https://github.com/android/storage-samples/tree/main/MediaStore

sdex
  • 3,169
  • 15
  • 28