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

相关内容

热门资讯

透视模拟器!aapoker破解... 透视模拟器!aapoker破解侠是真的吗(透视)透视插件(好像存在有挂);1、下载好aapoker破...
透视辅助!aapoker公共底... 透视辅助!aapoker公共底牌(透视)透视软件(其实是真的有挂)小薇(透视辅助)致您一封信;亲爱a...
透视数据!aapoker ai... 透视数据!aapoker ai插件(透视)脚本(竟然是有挂);1、游戏颠覆性的策略玩法,独创攻略技巧...
透视工具!aapoker辅助器... 透视工具!aapoker辅助器怎么用(透视)辅助(原来有挂)1、该软件可以轻松地帮助玩家将aapok...
透视安卓版!aapoker万能... 透视安卓版!aapoker万能辅助器(透视)发牌逻辑(好像真的是有挂)1、aapoker万能辅助器透...
透视最新!aapoker透视脚... 透视最新!aapoker透视脚本入口(透视)插件(一贯存在有挂)1、aapoker透视脚本入口ai辅...
透视好友房!aa poker辅... 透视好友房!aa poker辅助(透视)辅助插件工具(总是真的有挂);1、玩家可以在aa poker...
透视讲解!aapoker透视脚... 透视讲解!aapoker透视脚本入口(透视)万能辅助器(确实真的有挂)1)aapoker透视脚本入口...
透视肯定!aa poker透视... 透视肯定!aa poker透视软件(透视)辅助(一贯存在有挂)1、用户打开应用后不用登录就可以直接使...
透视攻略!aapoker辅助插... 透视攻略!aapoker辅助插件工具(透视)透视插件(好像有挂)运aapoker辅助插件工具辅助工具...