Android:从图库加载的位图在ImageView中旋转

当我将媒体库中的图像加载到位图中时,一切正常,除了将相机在垂直握住手机的情况下拍摄的照片旋转之外,即使在垂直方向上看起来是垂直的,我也总是得到水平的照片。画廊。为什么会这样,如何正确加载呢?



芜湖不芜
浏览 760回答 3
3回答

喵喵时光机

这是一个完整的解决方案(可从Facebook SDK的Hackbook示例中找到)。它的优点是不需要访问文件本身。如果要从内容解析器加载图像,这将非常有用(例如,如果您的应用正在响应共享照片的意图)。public static int getOrientation(Context context, Uri photoUri) {    /* it's on the external media. */    Cursor cursor = context.getContentResolver().query(photoUri,            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);    if (cursor.getCount() != 1) {        return -1;    }    cursor.moveToFirst();    return cursor.getInt(0);}然后,您可以如下获得旋转的位图。这段代码还将图片缩小(不幸的是)到MAX_IMAGE_DIMENSION。否则可能会耗尽内存。public static Bitmap getCorrectlyOrientedImage(Context context, Uri photoUri) throws IOException {    InputStream is = context.getContentResolver().openInputStream(photoUri);    BitmapFactory.Options dbo = new BitmapFactory.Options();    dbo.inJustDecodeBounds = true;    BitmapFactory.decodeStream(is, null, dbo);    is.close();    int rotatedWidth, rotatedHeight;    int orientation = getOrientation(context, photoUri);    if (orientation == 90 || orientation == 270) {        rotatedWidth = dbo.outHeight;        rotatedHeight = dbo.outWidth;    } else {        rotatedWidth = dbo.outWidth;        rotatedHeight = dbo.outHeight;    }    Bitmap srcBitmap;    is = context.getContentResolver().openInputStream(photoUri);    if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {        float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);        float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);        float maxRatio = Math.max(widthRatio, heightRatio);        // Create the bitmap from file        BitmapFactory.Options options = new BitmapFactory.Options();        options.inSampleSize = (int) maxRatio;        srcBitmap = BitmapFactory.decodeStream(is, null, options);    } else {        srcBitmap = BitmapFactory.decodeStream(is);    }    is.close();    /*     * if the orientation is not 0 (or -1, which means we don't know), we     * have to do a rotation.     */    if (orientation > 0) {        Matrix matrix = new Matrix();        matrix.postRotate(orientation);        srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),                srcBitmap.getHeight(), matrix, true);    }    return srcBitmap;}

郎朗坤

在这篇文章中,借助这篇文章帮助解决了我的问题:            Bitmap myBitmap = getBitmap(imgFile.getAbsolutePath());            try {                ExifInterface exif = new ExifInterface(imgFile.getAbsolutePath());                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);                Log.d("EXIF", "Exif: " + orientation);                Matrix matrix = new Matrix();                if (orientation == 6) {                    matrix.postRotate(90);                }                else if (orientation == 3) {                    matrix.postRotate(180);                }                else if (orientation == 8) {                    matrix.postRotate(270);                }                myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, myBitmap.getWidth(), myBitmap.getHeight(), matrix, true); // rotating bitmap            }            catch (Exception e) {            }            ImageView img = (ImageView) findViewById(R.id.imgTakingPic);            img.setImageBitmap(myBitmap);希望它可以节省别人的时间!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android