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

相关内容

热门资讯

透视分享!约局吧开挂神器是真的... 透视分享!约局吧开挂神器是真的吗(透视)wepoker脚本,教程方式(有挂攻略)-哔哩哔哩1、约局吧...
透视教你!哈糖大菠萝万能挂(透... 透视教你!哈糖大菠萝万能挂(透视)德州透视是真的假的,教程诀窍(新版有挂)-哔哩哔哩德州透视是真的假...
透视解密!wepoker开脚本... 透视解密!wepoker开脚本视频(透视)hhpoker底牌透视脚本,教程指南书(果真有挂)-哔哩哔...
透视教你!hhpoker开挂教... 透视教你!hhpoker开挂教程(透视)购买wepoker模拟器,教程方案(有挂秘诀)-哔哩哔哩亲,...
透视分享!wpk透视插件(透视... 透视分享!wpk透视插件(透视)红龙poker透视指令,教程绝活(有挂教程)-哔哩哔哩1、让任何用户...
透视详情!wepoker透视脚... 透视详情!wepoker透视脚本视频(透视)xpoker辅助工具,教程要领(有挂细节)-哔哩哔哩;1...
透视普及!wpk透视是真的假的... 透视普及!wpk透视是真的假的(透视)wepoker透视有吗,教程机巧(有挂辅助)-哔哩哔哩;1、w...
透视科普!破解辅助插件wepo... 透视科普!破解辅助插件wepoker(透视)wepoker私人局外卦,教程攻略(有挂助手)-哔哩哔哩...
透视详情!we-poker辅助... 透视详情!we-poker辅助器(透视)wpk透视,教程步骤(有人有挂)-哔哩哔哩1)we-poke...
透视总结!wepoker辅助是... 透视总结!wepoker辅助是真的吗(透视)wepoker怎么发冤家牌,教程要领(有挂技术)-哔哩哔...