图库中图像的方向始终为0

时间:2018-01-04 19:43:19

标签: android image orientation exif android-gallery

我正在尝试从ImageView中的图库加载图像。此代码在华为p10上正常工作,但在Samsung s5和其他设备上无法正常工作。当我加载图像时,方向不正确。 我正在使用此功能从图库加载,但方向始终为0。

public void loadPostImage(View v) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE1);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE1) {
            selectedImageUri = data.getData();
            if (null != selectedImageUri) {
                flag=true;
                path = selectedImageUri.getPath();
                int orientation = getOrientation(getApplicationContext(), selectedImageUri);
                Log.e("image path", path + "");
                image_post.setImageURI(selectedImageUri);
            }
        }
    }

}

private static int getOrientation(Context context, Uri photoUri) {
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, null, null, null);

    if (cursor.getCount() != 1) {
        cursor.close();
        return -1;
    }

    cursor.moveToFirst();
    int orientation = cursor.getInt(0);
    cursor.close();
    cursor = null;
    return orientation;
}

我也尝试过EXIF。但没有任何改变。

 public void controlRotate(Uri photoUri) throws IOException {
    ExifInterface exif = new ExifInterface(photoUri.getPath());
    int exifRotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
            ExifInterface.ORIENTATION_UNDEFINED);

    switch (exifRotation) {
        case ExifInterface.ORIENTATION_ROTATE_90: {
            float photoRotation = 90.0f;
            break;
        }
        case ExifInterface.ORIENTATION_ROTATE_180: {
            float photoRotation = 180.0f;
            break;
        }
        case ExifInterface.ORIENTATION_ROTATE_270: {
            float photoRotation = 270.0f;
            break;
        }
    }
}

我使用此代码段解决了问题

public void orientation(Uri selectedImageUri){
    Uri uri = selectedImageUri; // the URI you've received from the other app
    InputStream in = null;
    try {
        in = getContentResolver().openInputStream(uri);
        ExifInterface exifInterface = new ExifInterface(in);
        int rotation = 0;
        int orientation = exifInterface.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotation = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotation = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotation = 270;
                break;
        }
        // Now you can extract any Exif tag you want
        // Assuming the image is a JPEG or supported raw format
    } catch (IOException e) {
        // Handle any errors
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {}
        }
    }
}

0 个答案:

没有答案