Android10中使用图像Uri旋转Picasso图像存在问题。
创始人
2024-08-20 09:00:56
0

在Android 10中,无法直接访问应用程序之外的公共存储的图像文件。因此,必须先将图像文件复制到应用程序内部存储中,然后再使用Picasso加载该图像。同时,为了正确旋转图像,需要使用ExifInterface获取图像的方向,并将其应用于Picasso。以下是解决该问题的代码示例:

String filePath = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues).toString();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
   // Copy the image to the app's internal storage
   ContentResolver resolver = getContext().getContentResolver();
   try (InputStream is = resolver.openInputStream(uri)) {
       FileOutputStream os = getContext().openFileOutput("temp_image", Context.MODE_PRIVATE);
       FileUtils.copy(is, os);
   }

   // Load the image with Picasso and apply the correct rotation
   File file = new File(getContext().getFilesDir(), "temp_image");
   ExifInterface exif = new ExifInterface(file.getPath());
   int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

   Picasso.get()
           .load(file)
           .rotate(getRotationForImage(orientation))
           .into(imageView);
} else {
   Picasso.get()
           .load(uri)
           .into(imageView);
}

// Helper method to get the correct rotation angle
private static float getRotationForImage(int orientation) {
   float rotate = 0;
   switch (orientation) {
       case ExifInterface.ORIENTATION_ROTATE_270:
           rotate = 270;
           break;
       case ExifInterface.ORIENTATION_ROTATE_180:
           rotate = 180;
           break;
       case ExifInterface.ORIENTATION_ROTATE_90:
           rotate = 90;
           break;
   }
   return rotate;
}

相关内容

热门资讯

透视软件!hhpoker脚本(... 透视软件!hhpoker脚本(透视)开挂辅助app(其实是有挂)-哔哩哔哩所有人都在同一条线上,像星...
透视脚本!微扑克微乐辅助,we... 透视脚本!微扑克微乐辅助,wepoker透视有没有,窍门教程(确实有挂)-哔哩哔哩所有人都在同一条线...
透视私人房!wepoker轻量... 透视私人房!wepoker轻量版透视(透视)开挂辅助攻略(切实有挂)-哔哩哔哩wepoker轻量版透...
透视透视挂!hhpoker免费... 透视透视挂!hhpoker免费透视脚本,wepoker有辅助工具吗,大纲教程(都是是真的挂)-哔哩哔...
透视检测!wepoker有辅助... 透视检测!wepoker有辅助功能吗(透视)开挂辅助挂(其实真的是有挂)-哔哩哔哩亲,关键说明,we...
透视新版!有没有人wepoke... 透视新版!有没有人wepoker,we poker辅助器,妙计教程(好像是真的挂)-哔哩哔哩1、很好...
透视软件!wepoker分析(... 透视软件!wepoker分析(透视)开挂辅助插件(切实是有挂)-哔哩哔哩1、每一步都需要思考,不同水...
透视美元局!wepoker怎么... 透视美元局!wepoker怎么发冤家牌,德扑圈透视,指引教程(本来是真的挂)-哔哩哔哩1)wepok...
透视黑科技!wpk俱乐部有没有... 透视黑科技!wpk俱乐部有没有辅助(透视)开挂辅助插件(总是是真的挂)-哔哩哔哩wpk俱乐部有没有辅...
透视插件!德普之星透视免费,约... 透视插件!德普之星透视免费,约局吧开挂,总结教程(切实真的有挂)-哔哩哔哩1、玩家可以在德普之星透视...