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 德州 辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】...
透视指引!佛手在线大菠萝智能辅... 透视指引!佛手在线大菠萝智能辅助器(透视)pokermaster辅助器(辅助)一直是真的有神器(哔哩...
透视模板!约局吧开挂(透视)哈... 透视模板!约局吧开挂(透视)哈糖大菠萝万能挂(辅助)原来一直总是有软件(哔哩哔哩)一、哈糖大菠萝万能...
透视妙计!佛手大菠萝辅助(透视... 透视妙计!佛手大菠萝辅助(透视)wepoker安装教程(辅助)一贯一直总是有软件(哔哩哔哩)1、玩家...
透视手册!线上德州的辅助器是什... 透视手册!线上德州的辅助器是什么(透视)约局吧德州有挂吗(辅助)都是是真的有插件(哔哩哔哩)1、透视...
透视指南书!拱趴大菠萝怎么开挂... 透视指南书!拱趴大菠萝怎么开挂(透视)智星德州插件2024最新版(辅助)本来有软件(哔哩哔哩)1、很...
透视绝活!pokemmo辅助器... 透视绝活!pokemmo辅助器(透视)约局吧游戏挂(辅助)原来是有app(哔哩哔哩)该软件可以轻松地...
透视总结!竞技联盟透视插件(透... 透视总结!竞技联盟透视插件(透视)约局吧德州有挂吗(辅助)竟然是有攻略(哔哩哔哩)1、打开软件启动之...
透视积累!约局吧怎么看有没有挂... 您好,约局吧怎么看有没有挂这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很...