尝试从图库中获取图像的路径时,为什么会出现错误?

问题描述

我有一个应用程序,用户可以在其中拍照或从图库中获取图像,然后在画布上作画,有必要为我的应用程序执行的其他功能选择图像路径,但是我注意到一个问题在华为android 10中,从Google驱动器的文件夹中下载了一些图片,当我尝试从设备的下载文件夹中选择其中一张图片时,出现以下错误:“只有拥有者才能与待处理的媒体内容进行交互:/ / media / external / image / media / 1879“

如果我使用其他设备或模拟器,一切都很好,关于此错误的有趣之处在于,另一个文件夹中的图像可以正常工作;如果将下载的图像移到另一个文件夹中,则可以正常工作,也可以将它们返回到下载文件夹。

private void Nueva() {
        try {
            Intent pickIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            pickIntent.setType("image/*");
            startActivityForResult(pickIntent,PICK_IMAGE);
        }
        catch (Exception e){
            AlertDialog.Builder dialogo=new AlertDialog.Builder(MenuActivity.this);
            dialogo.setTitle("Error");
            dialogo.setMessage(e.getMessage());
            dialogo.setPositiveButton("OK",null);
            dialogo.create();
            dialogo.show();
        }
    }

 @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data) {
        //Despues de haber seleccionado la imagen o de haber toma una nueva imagen
        try {
            super.onActivityResult(requestCode,resultCode,data);
            ExifInterface exif = null;

            if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {//Por galeria
                imageUri = data.getData();
                bmp= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                path=getRealPathFromUri2(getContext(),imageUri);
                exif = new ExifInterface(path);
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_UNDEFINED);
                bmp=Lienzo.rotateBitmap(bmp,orientation);
                ....
            }

        } catch (FileNotFoundException e) {
            Log.e("Error","onActivityResult: "+e.getMessage());
        } catch (IOException e) {
            Log.e("Error","onActivityResult: "+e.getMessage());
        }catch (IllegalArgumentException e){
            Log.e("Error","onActivityResult: "+e.getMessage());
        }
        catch (Exception e){
            Log.e("Error","onActivityResult: "+e.getMessage());
        }

    }

public static String getRealPathFromUri2(Context context,Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri,proj,null,null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

我知道getRealPathFromUri2()代码在有问题的设备中可以正常工作,因为我在其他文件夹中多次使用它,问题在于从驱动器下载的新图像

...
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
...

解决方法

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

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...