Android – gets the direction of the image from mediastore.images.media.data

I have a mediastore.images.media.data URI for images. How can I use this URI to get mediastore.images.imagecolumns.orientation? I got a NullPointerException

Here's my code,

private  int getOrientation(Context context, Uri photoUri) {

Log.v("orientatioon", "not crashed01");
Cursor cursor = context.getContentResolver().query(photoUri,
        new String[] { MediaStore.Images.ImageColumns._ID,MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
Log.v("orientatioon", "not crashed02");


cursor.moveToFirst();
Log.v("orientatioon", "not crashed 03");
int i=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION));
Log.v("orientatioon", ""+i);
cursor.close();
return i;
}

I get a NullPointerException in the cursor. Movetofirst () line

resolvent:

Use this method to get the direction

public static int getExifOrientation(String filepath) {// YOUR MEDIA PATH AS STRING
        int degree = 0;
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(filepath);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (exif != null) {
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
            if (orientation != -1) {
                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;
                }

            }
        }
        return degree;
    }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>