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

相关内容

热门资讯

最新消息!pokerworld... 最新消息!pokerworld修改器(透视)皮皮辅助软件是真的吗(其实有辅助神器)-哔哩哔哩1、金币...
据相关数据显示!拱趴游戏破解器... 据相关数据显示!拱趴游戏破解器(透视)小程序牵手跑得辅助器下载(总是存在有辅助平台)-哔哩哔哩进入游...
针对!wpk软件是正规的吗(透... 针对!wpk软件是正规的吗(透视)宝宝浙江辅助器(总是存在有辅助安装)-哔哩哔哩1、起透看视 宝宝浙...
透视美元局!wepokerpl... 透视美元局!wepokerplus透视脚本免费(透视)邳州友友有没有辅助软件(竟然存在有辅助工具)-...
有玩家发现!wejoker辅助... 有玩家发现!wejoker辅助脚本(透视)指尖四川刷无限钻石(其实真的有辅助安装)-哔哩哔哩1、指尖...
透视数据!红龙poker辅助(... 透视数据!红龙poker辅助(透视)老友广东辅助工具下载(原来真的有辅助修改器)-哔哩哔哩所有人都在...
透视存在!来玩app破解版(透... 透视存在!来玩app破解版(透视)透视辅助器免费欢聚水鱼(果然存在有辅助软件)-哔哩哔哩;1、透视辅...
这一现象值得深思!hhpoke... 这一现象值得深思!hhpoker德州挂真的有吗(透视)河洛杠次插件(原来存在有辅助修改器)-哔哩哔哩...
事发当天!wepoker永久免... 事发当天!wepoker永久免费脚本(透视)小闲川南手游辅助器(真是存在有辅助安装)-哔哩哔哩1、该...
无独有偶!pokemmo脚本(... 无独有偶!pokemmo脚本(透视)新道游辅助器透视挂(其实是有辅助辅助器)-哔哩哔哩进入游戏-大厅...