次のコードを使用して選択した画像の向きを取得していますので、ギャラリーから選択すると縦に撮った場合は水平に表示されません。ギャラリーから選択したときに画像が回転しました
ファイルでエラーが発生しています。imageFile = new File(selectedImage.toString());パラメータselectedImage.toString()内で、最初のint rotate = 90を変更して垂直イメージを選択したので、結果は良好でした。
ファイルにパラメータを正しく渡していますか?
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
BitmapFactory.Options options;
if (resultCode == RESULT_OK && requestCode == PICTURE_SELECTED) {
try {
options = new BitmapFactory.Options();
options.inSampleSize = 4;
Uri selectedImage = data.getData();
InputStream stream = getContentResolver().openInputStream(selectedImage);
Bitmap yourSelectedImage = BitmapFactory.decodeStream(stream, null, options);
stream.close();
//orientation
try {
int rotate = 0;
try {
File imageFile = new File(selectedImage.toString());
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 (Exception e) {
e.printStackTrace();
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
yourSelectedImage = Bitmap.createBitmap(yourSelectedImage , 0, 0, yourSelectedImage.getWidth(), yourSelectedImage.getHeight(), matrix, true); }
catch (Exception e) {}
//end of orientation
imgButton.setScaleType(ImageView.ScaleType.FIT_CENTER);
imgButton.setImageBitmap(yourSelectedImage);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "Image was not selected", Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
画像は、この<活動 アンドロイドのようにギャラリーを開設しているあなたの活動中にあなたマニフェストにこの行を追加します。name =「ui.ui.activity.MainActivity。」 アンドロイド:configChangesは= "keyboardHidden |オリエンテーション| screenSize " android:label =" @ string/title_activity_main " android:screenOrientation =" portrait "/> –
このコードは少し役立ちましたか? –
いいえ、エラー:android:configChangesとscreenOrientationは許可されていません – cgeekmt