Выбор изображения всегда дает альбомное изображение

Я использовал следующий код для получения изображения из галереи телефона:

Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);
  startActivityForResult(Intent.createChooser(intent, "Select Picture"),
    USE_LIBRARY_PIC_REQUEST);

В onActivityResult() он дает путь к файлу, я пытался получить растровое изображение, используя путь к файлу, но он всегда дает его в альбомной ориентации. Есть ли способ всегда получать это изображение в портретном режиме? Код для получения пути к файлу:

Uri selectedImageUri = data.getData();
    selectedImagePath = getPath(selectedImageUri);

Вот как я получил растровое изображение, используя путь к файлу:

Bitmap bmp=BitmapFactory.decodeFile(selectedImagePath);

person Seshu Vinay    schedule 27.06.2012    source источник
comment
ваша проблема в том, что вы сделали фотографию в портретном режиме, а затем, когда она загружается, она загружается в альбомную? Или вы просто хотели повернуть все фото при загрузке на 90 градусов?   -  person Sudar Nimalan    schedule 27.06.2012


Ответы (1)


чтобы получить исходное изображение, вы можете использовать этот метод.

public static Bitmap getBitmap(String uri, Context mContext) {

    Bitmap bitmap = null;
    Uri actualUri = Uri.parse(uri);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inTempStorage = new byte[16 * 1024];
    options.inSampleSize = 2;
    ContentResolver cr = mContext.getContentResolver();
    float degree = 0;
    try {
        ExifInterface exif = new ExifInterface(actualUri.getPath());
        String exifOrientation = exif
                .getAttribute(ExifInterface.TAG_ORIENTATION);
        bitmap = BitmapFactory.decodeStream(cr.openInputStream(actualUri),
                null, options);
        if (bitmap != null) {
            degree = getDegree(exifOrientation);
            if (degree != 0)
                bitmap = createRotatedBitmap(bitmap, degree);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

public static float getDegree(String exifOrientation) {
    float degree = 0;
    if (exifOrientation.equals("6"))
        degree = 90;
    else if (exifOrientation.equals("3"))
        degree = 180;
    else if (exifOrientation.equals("8"))
        degree = 270;
    return degree;
}

public static Bitmap createRotatedBitmap(Bitmap bm, float degree) {
    Bitmap bitmap = null;
    if (degree != 0) {
        Matrix matrix = new Matrix();
        matrix.preRotate(degree);
        bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                bm.getHeight(), matrix, true);
    }

    return bitmap;
}
person Mohsin Naeem    schedule 27.06.2012
comment
exifOrientation всегда получает «0» - person sai; 24.07.2012