Skip to content

getExifOrientation does not close cursor on exception #216

@consp1racy

Description

@consp1racy

Move cursor declaration outside try block and close it when not null when exception is trown.

private int getExifOrientation(String sourceUri) {
    int exifOrientation = ORIENTATION_0;
    if (sourceUri.startsWith(ContentResolver.SCHEME_CONTENT)) {
        final String[] columns = { MediaStore.Images.Media.ORIENTATION };
        Cursor cursor = null;
        try {
            cursor = getContext().getContentResolver().query(Uri.parse(sourceUri), columns, null, null, null);
            // if (cursor != null) {
                if (cursor.moveToFirst()) {
                    int orientation = cursor.getInt(0);
                    if (VALID_ORIENTATIONS.contains(orientation) && orientation != ORIENTATION_USE_EXIF) {
                        exifOrientation = orientation;
                    } else {
                        Log.w(TAG, "Unsupported orientation: " + orientation);
                    }
                }
                cursor.close();
            // }
        } catch (Exception e) {
            Log.w(TAG, "Could not get orientation of image from media store");
            if (cursor != null) cursor.close();
        }
    } else if (sourceUri.startsWith(ImageSource.FILE_SCHEME) && !sourceUri.startsWith(ImageSource.ASSET_SCHEME)) {
        // ...
    }
    return exifOrientation;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions