简体   繁体   中英

RecoverableSecurityException no access to content://media/external/audio/media

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM