delete Not-Owned files in Android Q using Java code-ThrowExceptions

Exception or error:

How can we delete Not-Owned files in Android Q

I dont want to use SAF file picker

can we trigger directly SAF to delete file

or is there a better sollution

I came across this

if (Build.VERSION.SDK_INT >=  Build.VERSION_CODES.Q) {
                    try {
                        String uri1 = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString();
                        String where = MediaStore.Audio.Media._ID + "=?";
                        String[] selectionArgs = new String[]{String.valueOf(songforpopup._id)};
                        int deleted = getApplicationContext().getContentResolver().delete(Uri.parse(uri1), where, selectionArgs);
                        Toast.makeText(AllSongsActivity.this, "" + deleted, Toast.LENGTH_SHORT).show();
                        if (deleted>=0){
                            Toast.makeText(AllSongsActivity.this, "Succesfully deleted in Android Q",
                                    Toast.LENGTH_LONG).show();
                        }
                    } catch (SecurityException securityException) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                            RecoverableSecurityException recoverableSecurityException;
                            if (securityException instanceof RecoverableSecurityException) {
                                recoverableSecurityException =
                                        (RecoverableSecurityException)securityException;
                            } else {
                                throw new RuntimeException(
                                        securityException.getMessage(), securityException);
                            }
                            IntentSender intentSender =recoverableSecurityException.getUserAction()
                                    .getActionIntent().getIntentSender();
                            try {
                                startIntentSenderForResult(intentSender, DELETE_PERMISSION_REQUEST,
                                        null, 0, 0, 0, null);
                            } catch (IntentSender.SendIntentException e) {
                                e.printStackTrace();
                            }
                        } else {
                            throw new RuntimeException(
                                    securityException.getMessage(), securityException);
                        }
                    }

                    new AsyncListViewLoader().execute(Order);
                    timerDelayRunForScroll(100);
                }

but It doesn’t delete the file and neither does the security exception is thrown

How to solve:

Try:

Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);

int deleted = context.getContentResolver().delete(uri, null, null);

Leave a Reply

Your email address will not be published. Required fields are marked *