我试图通过以下代码在Android应用程序中查看来自url的图像:
img = (ImageView) view.findViewById(R.id.img);
new LoadImage().execute("http://localhost" + file_name);
效果很好,但它忽略了图像的EXIF数据,因此我的图像会旋转。如何根据EXIF数据查看图像?
答案 0 :(得分:1)
调用fixOrientation来修复图像方向
public static int getExifRotation(String imgPath) {
try {
ExifInterface exif = new ExifInterface(imgPath);
String rotationAmount = exif
.getAttribute(ExifInterface.TAG_ORIENTATION);
if (!TextUtils.isEmpty(rotationAmount)) {
int rotationParam = Integer.parseInt(rotationAmount);
switch (rotationParam) {
case ExifInterface.ORIENTATION_NORMAL:
return 0;
case ExifInterface.ORIENTATION_ROTATE_90:
return 90;
case ExifInterface.ORIENTATION_ROTATE_180:
return 180;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270;
default:
return 0;
}
} else {
return 0;
}
} catch (Exception ex) {
return 0;
}
}
public static Bitmap fixOrientation(String filePath, Bitmap bm) {
int orientation = getExifRotation(filePath);
if (orientation == 0 || orientation % 360 == 0) {
//it is already right orientation, no need to rotate
return bm;
}
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(),
matrix, true);
}
我建议您使用像Glide或Fresco这样的现代图像加载器,而不是直接使用AsyncTask处理图像。