コードなしで何が起こっているのかを知るのは難しいです。
私が見つけた最も簡単な方法は、EXIF情報を読んで、画像に回転が必要かどうかを確認することです。あなたはログがrotationForImage機能の「エラーはEXIFをチェックする」が表示されますエラーが発生した場合
/** An URI and a imageView */
public void setBitmap(ImageView mImageView, String imageURI){
// Get the original bitmap dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(imageURI, options);
float rotation = rotationForImage(getActivity(), Uri.fromFile(new File(imageURI)));
if(rotation!=0){
//New rotation matrix
Matrix matrix = new Matrix();
matrix.preRotate(rotation);
mImageView.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, reqHeight, reqWidth, matrix, true));
} else {
//No need to rotate
mImageView.setImageBitmap(BitmapFactory.decodeFile(imageURI, options));
}
}
/** Returns how much we have to rotate */
public static float rotationForImage(Context context, Uri uri) {
try{
if (uri.getScheme().equals("content")) {
//From the media gallery
String[] projection = { Images.ImageColumns.ORIENTATION };
Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
if (c.moveToFirst()) {
return c.getInt(0);
}
} else if (uri.getScheme().equals("file")) {
//From a file saved by the camera
ExifInterface exif = new ExifInterface(uri.getPath());
int rotation = (int) exifOrientationToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
return rotation;
}
return 0;
} catch (IOException e) {
Log.e(TAG, "Error checking exif", e);
return 0;
}
}
/** Get rotation in degrees */
private static float exifOrientationToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270;
}
return 0;
}
:Android上でExifInterfaceクラスの詳細を読むには: 言っhttp://developer.android.com/intl/es/reference/android/media/ExifInterface.html
、ここではいくつかのサンプルコードです。
答えは、ExifInterfaceの方向データをチェックする方法を説明するのに良いですが、それは元の質問ではないようです。 「向きフィールドを正しく記録しないデバイスはどう対処しますか?」質問のジストであるようです。この回答は、アンドロイドのドキュメントで明示的に説明されているExifInterfaceから方向フィールドを取得する方法についてのみ説明しています。 –
私のためには動作しません。 –