CysionLiu / ImagePicker

仿微信的图片选择,支持AndroidX,适配Android Q
Apache License 2.0
321 stars 71 forks source link

部分手机拿不出宽高,部分手机图片旋转了,导致宽高相反 #13

Open msrlin opened 5 years ago

msrlin commented 5 years ago

部分手读取宽高出错,部分手机图片旋转了,导致宽高相反

CysionLiu commented 5 years ago

手头的测试机没发现这问题。是三星的机型吗?

CysionLiu commented 5 years ago

应该是三星,这机型的相机就是秀。。

msrlin commented 5 years ago

应该是三星,这机型的相机就是秀。。

是三星的机型,还有部分手机无法获取宽高,我目前做了如下处理

    int degree = readPictureDegree(imageItem.path);//获取旋转角度

    Matrix matrix = new Matrix();
    matrix.postRotate(degree);

    if (imageItem.width == 0 || imageItem.height == 0) {//部分手机文件会显示0
        Bitmap bitmap = BitmapFactory.decodeFile(imageItem.path);
        Log.e("WWWWWWWWW/HHHHHH", bitmap.getWidth() + ":" + bitmap.getHeight());

        if (bitmap != null) {
            imageItem.width = bitmap.getWidth();
            imageItem.height = bitmap.getHeight();

            if (imageItem.width == 0 || imageItem.height == 0) {
                toast(imageItem.name + "文件获取失败,已忽略此照片");
                return;
            }

        } else {
            toast(imageItem.name + "文件获取失败,已忽略此照片");
            return;
        }
    }

    int imgWidth = imageItem.width;
    int imgHeight = imageItem.height;

    if (degree != 0) {
        if (degree == 90 || degree == 270) {//出现旋转情况,进行宽高互换
            imgWidth = imageItem.height;
            imgHeight = imageItem.width;
        }
    }

/** * 读取图片属性:旋转的角度 * @param path 图片绝对路径
 * @return degree旋转的角度
 */

public static int readPictureDegree(String path) {

    int degree = 0;
    try {
        ExifInterface exifInterface = new ExifInterface(path);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
            ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    return degree;
}