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

相关内容

热门资讯

微扑克辅助机器人!微扑克ai辅... 您好,微扑克这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游戏中...
wpk提高胜率!wpk透视,W... wpk提高胜率!wpk透视,Wpk其实真的有挂,详细教程,(有挂脚本)运wpk辅助工具,进入游戏界面...
wepoke智能ai!德扑之星... wepoke智能ai!德扑之星可以操作,aapoker有手游版(详细辅助教程)1、每一步都需要思考,...
2025新版技巧!wepoke... 2025新版技巧!wepoke小技巧(wepoke辅助德之星)WePoKer(原来真的有挂) 科技详...
微扑克辅助软件!微扑克网页版辅... 微扑克辅助软件!微扑克网页版辅助,wpk好像是真的有挂,AI教程(有挂插件)1、在微扑克ai机器人技...
wpk德州透视辅助!wPK原来... wpk德州透视辅助!wPK原来真的有挂,wPK的确是有挂的,普及教程(有挂脚本)1)wpk辅助挂:进...
德州ai辅助有用!微扑克德州专... 德州ai辅助有用!微扑克德州专用辅助器游戏平台,wopoker辅助v3.0(详细透视教程);玩家在微...
微扑克德州专用辅助器!微扑克w... 微扑克德州专用辅助器!微扑克wpk辅助软件,微扑克俱乐部24小时(切实有挂辅助挂)是一款可以让一直输...
wepokeai代打的胜率!w... wepokeai代打的胜率!wopoker有外 挂(辅助透视)wePokE(一般真的有挂);《WPK...
wpk有辅助挂(WPK)wpk... wpk有辅助挂(WPK)wpk透视辅助测试(透视)本来真的有挂(攻略教程)小薇(透视辅助)致您一封信...