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脚本(透视)新上游辅助器(都是存在有辅助插件)-哔哩哔哩1、进入游戏-大厅...
透视教学!wepoker俱乐部... 透视教学!wepoker俱乐部辅助(透视)吉祥填大坑插件(其实真的是有辅助工具)-哔哩哔哩1、点击下...
透视软件!哈糖大菠萝软件下载(... 透视软件!哈糖大菠萝软件下载(透视)雀神麻雀充运势用吗(其实是真的辅助修改器)-哔哩哔哩一、雀神麻雀...
今年以来!佛手在线有挂吗(透视... 今年以来!佛手在线有挂吗(透视)789大菠萝攻略(都是有辅助工具)-哔哩哔哩1、很好的工具软件,可以...
在玩家背景下!wepoker有... 在玩家背景下!wepoker有插件吗(透视)微信闲来辅助神器免费(真是是真的辅助下载)-哔哩哔哩微信...
透视透视挂!aapoker插件... 透视透视挂!aapoker插件(透视)广东雀神智能插件是真的(好像存在有辅助神器)-哔哩哔哩1、金币...
一直以来!智星菠萝可以辅助吗(... 一直以来!智星菠萝可以辅助吗(透视)小闲辅助神器(其实是真的辅助工具)-哔哩哔哩一直以来!智星菠萝可...
透视好友房!黑侠破解wepok... 透视好友房!黑侠破解wepoker(透视)兴动互娱辅助(好像有辅助安装)-哔哩哔哩1.兴动互娱辅助 ...
事发当天!hhpoker底牌透... 事发当天!hhpoker底牌透视脚本(透视)微乐家乡破解版(其实存在有辅助软件)-哔哩哔哩1、实时微...
透视代打!hhpoker德州透... 透视代打!hhpoker德州透视(透视)微乐小程序多功能修改器(一贯有辅助辅助器)-哔哩哔哩1、在微...