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

相关内容

热门资讯

透视了解"aapok... 透视了解"aapoker插件"详细辅助揭秘攻略(有透视底牌软件);aapoker插件软件透明挂更新新...
透视脚本"约局吧德州... 1、透视脚本"约局吧德州透视"详细辅助力荐教程(代打辅助技巧)2、进入游戏-大厅左侧-新手福利-激活...
透视存在"wepok... 1、透视存在"wepoker底牌透视"详细辅助介绍教程(作弊辅助辅助)。2、wepoker底牌透视透...
透视免费"德州局脚本... 透视免费"德州局脚本"详细辅助辅助教程(俱乐部辅助透明);实战中需综合运用上述技巧,并根据牌桌动态灵...
透视能赢"wepok... 透视能赢"wepoker永久免费脚本"详细辅助存在挂教程(可以开透视辅助);大神普及一款德州ai内幕...
透视透视挂"拱趴大菠... 透视透视挂"拱趴大菠萝万能挂"详细辅助高科技教程(私人局辅助神器)是一款可以让一直输的玩家,快速成为...
透视教学"aapok... 透视教学"aapoker辅助软件合法吗"详细辅助可靠教程(有透视方法透明);1.aapoker辅助软...
透视好友房"wepo... 透视好友房"wepoker祈福有用吗"详细辅助大神讲解(软件靠谱么技巧)科技教程也叫必备教程,这是一...
透视透视"wpk插件... 透视透视"wpk插件"详细辅助可靠技巧(模拟器哪个攻略)科技教程也叫必备教程,这是一款功能强大的wp...
透视神器"xpoke... 1、透视神器"xpoker怎么作弊"详细辅助AA德州教程(辅助器官方透视)。2、xpoker怎么作弊...