0

我有一个应用程序,从相机或画廊拍摄的照片,并在imageview中显示结果。所有垂直方向的图像奇怪自动只在某些设备

我只得到与内容提供商的图像,并使用这个尺度功能

public Bitmap scaleim(Bitmap bitmap) { 
     ... 
     Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, resizedWidth, resizedHeight, false); 
     return scaledBitmap; 
    } 

在我的设备与Android 5一切工作确定,我现在已经与Android 7测试相同的应用程序一个我的朋友设备上,和每个垂直方向的图片都会自动旋转到水平方向。 这看起来很奇怪,我不知道是什么原因引起的问题。

回答

0

问题不在缩放中,但捕获的图像按硬件工作方式不同。在开始缩放之前,应根据合适的设备进行旋转。 这是下面的代码:

Matrix matrix = new Matrix(); 
    matrix.postRotate(getImageOrientation(url)); 
    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), 
    bitmap.getHeight(), matrix, true) 

public static int getImageOrientation(String imagePath){ 
    int rotate = 0; 
    try { 

     File imageFile = new File(imagePath); 
     ExifInterface exif = new ExifInterface(
       imageFile.getAbsolutePath()); 
     int orientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 

     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; 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return rotate; 
} 
+0

感谢您的回答,但我不能找到有效的方式来获得使用这种方法的URI的文件路径,并返回我FileNotFoundException异常。你有什么建议吗? – AndreaF

+0

我相信它应该有路径,然后才转向位图。 scaleim(位图位图) – QuokMoon