【问题标题】:Picking image always gives landscape image拾取图像总是给出风景图像
【发布时间】:2012-06-28 12:40:24
【问题描述】:

我已使用以下代码从手机图库中获取图片:

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);

【问题讨论】:

  • 您的问题是“您以纵向模式拍摄照片,然后在加载时,它会加载到横向?”或者“您只是想在加载 90 度时旋转所有照片?”

标签: android file bitmap image-gallery


【解决方案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;
}

【讨论】:

  • exifOrientation 总是得到 '0'
猜你喜欢
  • 1970-01-01
  • 2013-07-28
  • 2022-01-24
  • 1970-01-01
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
相关资源
最近更新 更多