问题
I load different images in my RecyclerView and I use Picasso to get them from internet, but randomly an image from iPhone is getting rotated upside down and I don't understand why.
It looks fine on the internet.
my code is:
Picasso.with(context)
.load(URLConstants.URL_BASE + imageURL)
.placeholder(image)
.error(image)
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Matrix matrix = new Matrix();
try {
ExifInterface exif = new ExifInterface(URLConstants.URL_BASE + imageURL);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Log.d("EXIF", "Exif: " + orientation);
if (orientation == 6) {
matrix.setRotate(90);
} else if (orientation == 3) {
matrix.setRotate(180);
} else if (orientation == 8) {
matrix.setRotate(270);
}
} catch (Exception e) {
e.printStackTrace();
}
Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
imageView.setImageBitmap(oriented);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
I don't know if
ExifInterface exif = new ExifInterface(URLConstants.URL_BASE + imageURL);
works with URLs or not.
And if they don't, what other way do I have?
回答1:
Right. This method will not work with URL.
ExifInterface exif = new ExifInterface(URLConstants.URL_BASE + imageURL);
you should check this control on downloaded Bitmap.In this case it is not check downloaded one. You can save it like temp.jpeg. Beside It is the method in android ExifInterface.java, it says like that on comment.
/**
* Reads Exif tags from the specified JPEG file.
*/
public ExifInterface(String filename) throws IOException {
if (filename == null) {
throw new IllegalArgumentException("filename cannot be null");
}
mFilename = filename;
loadAttributes();
}
Edited
You can check this class for proper orientation https://github.com/eralpyucel/ProfileImage/blob/master/app/src/main/java/com/eralp/profilephoto/PhotoFunctions.java
来源:https://stackoverflow.com/questions/37306866/random-image-gets-rotated-when-using-picasso