使用Java代码删除Android Q中的非拥有文件

问题描述 投票:0回答:1

我们如何删除Android Q中的非拥有文件

我不想使用SAF文件选择器

我们能否直接触发SAF删除文件

或者有更好的解决方法

我碰到了这个

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

但是它不会删除文件,也不会引发安全异常

java android delete-file android-10.0
1个回答
0
投票
尝试:

Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id); int deleted = context.getContentResolver().delete(uri, null, null);

© www.soinside.com 2019 - 2024. All rights reserved.