Android : How to detect the image orientation (portrait or landscape) picked from gallery while setting on an imageview? -


i setting image on imageview picked gallery(camera album). if picked image has landscape orientation, displays if image in in portrait mode(i.e image clicked in portrait mode) displaying image 90 degree rotation. trying find out orientation before setting on imageview, images giving same orientation , same width-height. here code :

uri selectedimage = intent.getdata(); if (selectedimage != null) {     bitmap bitmap = mediastore.images.media.getbitmap(this.getcontentresolver(), selectedimage);      int str = new exifinterface(selectedimage.getpath()).getattributeint("orientation", 1000);     toast.maketext(this, "value:" + str, toast.length_long).show();     toast.maketext(this, "width:" + bitmap.getwidth() + "height:" + bitmap.getheight(), toast.length_long).show(); 

portrait mode landscape mode

use exifinterface rotate image. use method correct value rotate captured image camera.

public int getcameraphotoorientation(context context, uri imageuri, string imagepath){     int rotate = 0;     try {         context.getcontentresolver().notifychange(imageuri, null);         file imagefile = new file(imagepath);          exifinterface exif = new exifinterface(imagefile.getabsolutepath());         int orientation = exif.getattributeint(exifinterface.tag_orientation, exifinterface.orientation_normal);          switch (orientation) {         case exifinterface.orientation_rotate_270:             rotate = 270;             break;         case exifinterface.orientation_rotate_180:             rotate = 180;             break;         case exifinterface.orientation_rotate_90:             rotate = 90;             break;         }          log.i("rotateimage", "exif orientation: " + orientation);         log.i("rotateimage", "rotate value: " + rotate);     } catch (exception e) {         e.printstacktrace();     }     return rotate; } 

and put code in activity result method , value rotate image...

string selectedimage = data.getdata(); string[] filepathcolumn = {mediastore.images.media.data};  cursor cursor = getcontentresolver().query(selectedimage, filepathcolumn, null, null, null); cursor.movetofirst();  int columnindex = cursor.getcolumnindex(filepathcolumn[0]); filepath = cursor.getstring(columnindex); cursor.close();  int rotateimage = getcameraphotoorientation(myactivity.this, selectedimage, filepath); 

hope helps..


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -