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

相关内容

热门资讯

曝光透视!拱趴大菠萝万能挂!竟... 曝光透视!拱趴大菠萝万能挂!竟然是真的有辅助软件(发现有挂)-哔哩哔哩拱趴大菠萝万能挂辅助器是一种具...
关于透视!德普之星app安卓版... 关于透视!德普之星app安卓版破解版,哈糖大菠萝有挂吗5个常用方法,办法教程(了解有挂)-哔哩哔哩1...
详细透视!德州透视是真的吗!一... 详细透视!德州透视是真的吗!一直是有辅助技巧(果真有挂)-哔哩哔哩德州透视是真的吗能透视中分为三种模...
辅助透视!wpk俱乐部怎么作必... 辅助透视!wpk俱乐部怎么作必弊,xpoker辅助,模块教程(真是有挂)-哔哩哔哩wpk俱乐部怎么作...
普及透视!德州局透视脚本下载安... 普及透视!德州局透视脚本下载安装最新版本!一贯是有辅助教程(有挂秘籍)-哔哩哔哩1、上手简单,内置详...
普及透视!wepoker辅助工... 普及透视!wepoker辅助工具,hardrock作必弊,项目教程(有挂详情)-哔哩哔哩1、起透看视...
详情透视!pokemmo脚本!... 详情透视!pokemmo脚本!都是一直都是有辅助技巧(有挂教程)-哔哩哔哩1、许多玩家不知道poke...
必备透视!hhpoker外开挂... 必备透视!hhpoker外开挂靠谱吗,wepoker怎么发冤家牌,秘籍教程(发现有挂)-哔哩哔哩;在...
解迷透视!pokemmo脚本辅... 解迷透视!pokemmo脚本辅助器!一贯是真的有辅助教程(有挂细节)-哔哩哔哩1、pokemmo脚本...
必备透视!wepoker可以免... 必备透视!wepoker可以免费玩吗,wejoker辅助器要钱玩吗,教材教程(真的有挂)-哔哩哔哩1...