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

相关内容

热门资讯

透视解迷"wepok... 透视解迷"wepoker有辅助吗"一直存在有辅助技巧(哔哩哔哩)在进入wepoker有辅助吗软件靠谱...
透视关于"wepok... 透视关于"wepoker透视破解版"确实有辅助器(哔哩哔哩)1、进入游戏-大厅左侧-新手福利-激活码...
透视总结"德州透视插... 透视总结"德州透视插件"好像是真的辅助器(哔哩哔哩)1、首先打开德州透视插件辅助器下载最新版本,在德...
透视透视"pokem... 透视透视"pokemomo辅助软件"总是是有辅助app(哔哩哔哩)1、用户打开应用后不用登录就可以直...
透视有挂"hhpok... 透视有挂"hhpoker真的有透视吗"果然真的有辅助方法(哔哩哔哩)1、不需要AI权限,帮助你快速的...
透视揭幕"wepok... 透视揭幕"wepoker有透视吗"竟然有辅助方法(哔哩哔哩)1、玩家可以在wepoker有透视吗透视...
透视曝光"pokem... 透视曝光"pokemmo辅助脚本"确实真的是有辅助技巧(哔哩哔哩)一、pokemmo辅助脚本游戏安装...
透视关于"wpk辅助... 透视关于"wpk辅助是什么"一直是真的辅助方法(哔哩哔哩)1、wpk辅助是什么免费辅助多个强度级别选...
透视解密"扑克之星辅... 您好,扑克之星辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在...
透视必备"wejok... 透视必备"wejoker辅助器要钱玩吗"一直有辅助攻略(哔哩哔哩)1、不需要AI权限,帮助你快速的进...