我如何一次只能使用getContentResolverupdate处理RecoverableSecurityException

问题描述

这是代码的一部分。()

uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {
            MediaStore.MediaColumns.RELATIVE_PATH,MediaStore.MediaColumns._ID,MediaStore.MediaColumns.disPLAY_NAME,MediaStore.MediaColumns.DATE_ADDED,};

String where = MediaStore.Images.Media.MIME_TYPE + "='image/jpeg'";

Cursor cursor = getContentResolver().query(uri,projection,where,null,MediaStore.MediaColumns.DATE_ADDED + " DESC");


int columndisplayname = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.disPLAY_NAME);
int columnDate = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_ADDED); 
int columnId = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID); 

String relativeLocation = Environment.DIRECTORY_PICTURES+ File.separator+"new folder";

while (cursor.movetoNext()){

    int columnId = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID);
    Long IdOfImage = cursor.getLong(columnId);
    Uri  uriimage = Uri.withAppendedpath(uri,""+IdOfImage);


    try{
        ParcelFileDescriptor parcelFileDescriptor = contentResolver.openFileDescriptor(uriimage,"r");
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        contentValues.put(MediaStore.MediaColumns.disPLAY_NAME,nameOfFile);
        contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH,relativeLocation);
        getContentResolver().update(uriimage,contentValues,null);


    }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,2,null);

            }catch(IntentSender.SendIntentException e){
                  e.printstacktrace();
            }
        } else {
            throw new RuntimeException(securityException.getMessage(),securityException);
        }
    }catch(FileNotFoundException e){
        e.printstacktrace();

    }

}

代码适用于android q版本。(sdkversion 30及更高版本)

光标在画廊的整个图像中循环。

在循环时,我想将用于将图像移动到满足条件语句的其他gallery文件夹的图像的相对路径。(从编写的代码中省略了此内容。)

我想将图像移到其他gallery文件夹,所以我尝试使用getContentResolver()。update()更改MediaStore.MediaColumns.RELATIVE_PATH标记

但是它对照片数量提出了过多的许可请求。( 也就是说,每当我使用更新功能更改图像的路径时,我都必须请求每个图像的许可。)

即使我更新了多张图像,我也必须先请求许可,然后再不想获得许可。

有没有一种方法可以将许可请求数量减少一次?

感谢您阅读,请回答我的问题

请参考链接https://developer.android.google.cn/training/data-storage/shared/media#update-other-apps-files

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)