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

相关内容

热门资讯

记者揭秘(约局吧)外挂透明挂辅... 记者揭秘(约局吧)外挂透明挂辅助挂(软件透明挂)的确是有挂的(2025已更新)(头条)1)辅助挂:进...
揭秘一下(扑克王)外挂透明挂辅... 揭秘一下(扑克王)外挂透明挂辅助安装(智能ai代打)一般真的有挂(2025已更新)(哔哩哔哩)所有人...
玩家科普(来玩德州)外挂透明挂... 玩家科普(来玩德州)外挂透明挂辅助app(透视)好像真的有挂(2023已更新)(微博热搜)1、全新机...
重要通知(Aapoker)外挂... 重要通知(Aapoker)外挂透明挂辅助APP(透视)一般真的有挂(2024已更新)(微博热搜)1、...
技术分享(扑克世界)外挂透明挂... 技术分享(扑克世界)外挂透明挂辅助插件(软件透明挂)一直真的有挂(2022已更新)(抖音)辅助器中分...
分享实测(wpK)外挂透明挂辅... 分享实测(wpK)外挂透明挂辅助机制(软件透明挂)的确是有挂的(2021已更新)(百度)1、下载好辅...
玩家必备科技(wepOke)外... 玩家必备科技(wepOke)外挂透明挂辅助黑科技(智能ai代打)一贯真的有挂(2022已更新)(哔哩...
盘点十款(aapokER)外挂... 盘点十款(aapokER)外挂透明挂辅助挂(智能ai代打)果真真的有挂(2023已更新)(头条)辅助...
揭秘关于(governorof... 揭秘关于(governorofpoker3)外挂透明挂辅助APP(透视辅助)原来真的有挂(2025已...
一分钟了解(德州)外挂透明挂辅... 一分钟了解(德州)外挂透明挂辅助工具(透视)其实真的有挂(2021已更新)(今日头条);1、每一步都...