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

相关内容

热门资讯

透视了解!aapoker辅助器... 透视了解!aapoker辅助器是真的吗,aapoker透视辅助,攻略方法(一贯是真的有挂);人气非常...
透视系统!德扑之星ai代打,德... 透视系统!德扑之星ai代打,德扑之星软件有猫腻吗,揭秘攻略(真是有挂)1、很好的工具软件,可以解锁游...
透视工具!微扑克有辅助透视,微... 透视工具!微扑克有辅助透视,微扑克ai辅助神器,系统教程(一向真的是有挂);无聊就玩这款微扑克有辅助...
透视最新!aapoker辅助,... 透视最新!aapoker辅助,aapoker透明挂真假,2025教程(从来真的有挂);人气非常高,a...
透视模拟器!智星德州菠萝辅助器... 透视模拟器!智星德州菠萝辅助器推荐,智星德州菠萝辅助器免费下载,靠谱教程(好像真的是有挂);透视模拟...
透视插件!wpk透视挂会被封号... 您好,wpk透视挂会被封号吗这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多...
透视黑科技!红龙扑克辅助器第一... 透视黑科技!红龙扑克辅助器第一视角,红龙扑克机制,wpk教程(原生是真的有挂);透视黑科技!红龙扑克...
透视智能ai!微扑克如何让系统... 您好,微扑克如何让系统发好牌这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多...
透视安卓版!德扑ai软件购买,... 透视安卓版!德扑ai软件购买,德扑之星软件辅牌器,解密教程(好像是真的有挂)1、金币登录送、破产送、...
透视系统!wepoke怎么看有... 透视系统!wepoke怎么看有没有外挂,wepoke ai辅助,细节方法(往昔真的是有挂);致您一封...