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

相关内容

热门资讯

大神推荐!博乐填大坑辅助器通用... 大神推荐!博乐填大坑辅助器通用版(外挂辅助工具)详细教程(有挂透明)-哔哩哔哩关于博乐填大坑辅助器通...
免费测试版!中至上饶小程序有挂... 免费测试版!中至上饶小程序有挂的(外挂辅助插件)详细教程(有挂透明)-哔哩哔哩;亲,其实确实真的有挂...
总算了解!创思维激k辅助器(外... 总算了解!创思维激k辅助器(外挂辅助软件)详细教程(有挂透明)-哔哩哔哩;一、创思维激k辅助器AI软...
指导大家!蜜瓜大厅有挂的(外挂... 指导大家!蜜瓜大厅有挂的(外挂辅助神器)详细教程(有挂技巧)-哔哩哔哩;原来确实真的有挂(需添加指定...
透明肯定!开元辅助器下载(外挂... 透明肯定!开元辅助器下载(外挂辅助APP)详细教程(有挂透明)-哔哩哔哩是一款可以让一直输的玩家,快...
一分钟了解!大晋斗地主随意选牌... 自定义大晋斗地主随意选牌系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不...
专业讨论!aapoker下载教... 专业讨论!aapoker下载教程(外挂辅助器)详细教程(有挂方法)-哔哩哔哩;一、aapoker下载...
一分钟揭秘!八闽十三水有外挂的... 一分钟揭秘!八闽十三水有外挂的(外挂辅助工具)详细教程(有挂方法)-哔哩哔哩1、许多玩家不知道八闽十...
透明科技!对战互娱有挂的(外挂... 此外,数据分析德州(对战互娱有挂的)辅助神器app还具备辅助透视行为开挂功能,通过对客户对战互娱有挂...
今日重大通报!青龙辅助软件(外... 今日重大通报!青龙辅助软件(外挂辅助挂)详细教程(有挂教程)-哔哩哔哩;玩家在青龙辅助软件中需先进行...