Need help to show image from gallery only in portrait mode Android -


i new bie in android development. please bare english. struck problem unable show image gallery in portrait mode,it showing in landscape mode.here code reference,could please me solve issue.thanks in advance.

what tried sofar is, have gone through below stackoverflow answers same issue , tried place same code in class image still sits in landscape mode only. stackvoerflow links followed are: 1.open image gallery in potrait mode

2.http://codereply.com/answer/5xf8di/android-detect-image-orientation-portrait-landscape-picked-gallery-setting-imageview.html

3.android: bitmaps loaded gallery rotated in imageview

public class fragmenttab1 extends fragment  {      relativelayout homes;       private static int result_load_image = 1;         imageview bitmapview;         bitmapfactory.options options;         string filepath;         button rot;     private int mdegree = 0;         @override         public view oncreateview(layoutinflater inflater, viewgroup container,                 bundle savedinstancestate) {             // view fragmenttab1.xml             view ima = inflater.inflate(r.layout.fragmenttab1, container, false);              homes = (relativelayout) ima.findviewbyid(r.id.hmt);                        bitmapview = (imageview) ima.findviewbyid(r.id.image);             homes.setonlongclicklistener(new onlongclicklistener() {                  @override                 public boolean onlongclick(view v) {                     intent = new intent(                             intent.action_pick,                             android.provider.mediastore.images.media.external_content_uri);                     startactivityforresult(i, result_load_image);                        return false;                 }             });             sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences(getactivity());             final string filepath = prefs.getstring("profilepic", "");             if (!filepath.equals("")) {                 bitmapview = (imageview) ima.findviewbyid(r.id.image);                 bitmapview.setimagebitmap(bitmaploaderf.loadbitmap(filepath, 500, 500));                 bitmapview.setscaletype(imageview.scaletype.fit_xy);                 }               return ima;         }         @override         public void onactivityresult(int requestcode, int resultcode, intent data) {             super.onactivityresult(requestcode, resultcode, data);             if (requestcode == result_load_image && resultcode == getactivity().result_ok && null != data) {                 uri picuri = data.getdata();                 string[] filepathcolumn = {mediastore.images.media.data};                 cursor cursor = getactivity().getcontentresolver().query(picuri,                         filepathcolumn, null, null, null);                 cursor.movetofirst();                 int columnindex = cursor.getcolumnindex(filepathcolumn[0]);                 filepath = cursor.getstring(columnindex);                 cursor.close();                 bitmapview.setimagebitmap(bitmaploaderf.loadbitmap(filepath, 500, 500));                 bitmapview.setscaletype(imageview.scaletype.fit_xy);              sharedpreferences shre = preferencemanager.getdefaultsharedpreferences(getactivity());             editor edit = shre.edit();             edit.putstring("profilepic", filepath);             edit.commit();             }         }  }     class bitmaploaderf {         public static int getscale(int originalwidth, int originalheight,                                    final int requiredwidth, final int requiredheight) {             //a scale of 1 means original dimensions             //of image maintained             int scale = 1;              //calculate scale if height or width of             //the image exceeds required value.             if ((originalwidth > requiredwidth) || (originalheight > requiredheight)) {                 //calculate scale respect                 //the smaller dimension                 if (originalwidth < originalheight)                     scale = math.round((float) originalwidth / requiredwidth);                 else                     scale = math.round((float) originalheight / requiredheight);              }              return scale;         }          public static bitmapfactory.options getoptions(string filepath,                                                        int requiredwidth, int requiredheight) {              bitmapfactory.options options = new bitmapfactory.options();             //setting injustdecodebounds true             //ensures able measure             //the dimensions of image,without             //actually allocating memory             options.injustdecodebounds = true;              //decode file measurement             bitmapfactory.decodefile(filepath, options);              //obtain insamplesize loading             //scaled down version of image.             //options.outwidth , options.outheight             //are measured dimensions of             //original image             options.insamplesize = getscale(options.outwidth,                     options.outheight, requiredwidth, requiredheight);              //set injustdecodebounds false again             //so can allocate             //bitmap memory             options.injustdecodebounds = false;              return options;          }           public static bitmap loadbitmap(string filepath,                                         int requiredwidth, int requiredheight) {               bitmapfactory.options options = getoptions(filepath,                     requiredwidth, requiredheight);              return bitmapfactory.decodefile(filepath, options);         }     } 

this :

public class exifutils { public static bitmap rotatebitmap(string src, bitmap bitmap) {     try {         int orientation = getexiforientation(src);          if (orientation == 1) {             return bitmap;         }          matrix matrix = new matrix();         switch (orientation) {         case 2:             matrix.setscale(-1, 1);             break;         case 3:             matrix.setrotate(180);             break;         case 4:             matrix.setrotate(180);             matrix.postscale(-1, 1);             break;         case 5:             matrix.setrotate(90);             matrix.postscale(-1, 1);             break;         case 6:             matrix.setrotate(90);             break;         case 7:             matrix.setrotate(-90);             matrix.postscale(-1, 1);             break;         case 8:             matrix.setrotate(-90);             break;         default:             return bitmap;         }          try {             bitmap oriented = bitmap.createbitmap(bitmap, 0, 0,                     bitmap.getwidth(), bitmap.getheight(), matrix, true);             bitmap.recycle();             return oriented;         } catch (outofmemoryerror e) {             e.printstacktrace();             return bitmap;         }     } catch (ioexception e) {         e.printstacktrace();     }      return bitmap; }  private static int getexiforientation(string src) throws ioexception {     int orientation = 1;      try {         /**          * if targeting api level >= 5 exifinterface exif =          * new exifinterface(src); orientation =          * exif.getattributeint(exifinterface.tag_orientation, 1);          */         if (build.version.sdk_int >= 5) {             class<?> exifclass = class                     .forname("android.media.exifinterface");             constructor<?> exifconstructor = exifclass                     .getconstructor(new class[] { string.class });             object exifinstance = exifconstructor                     .newinstance(new object[] { src });             method getattributeint = exifclass.getmethod("getattributeint",                     new class[] { string.class, int.class });             java.lang.reflect.field tagorientationfield = exifclass                     .getfield("tag_orientation");             string tagorientation = (string) tagorientationfield.get(null);             orientation = (integer) getattributeint.invoke(exifinstance,                     new object[] { tagorientation, 1 });         }     } catch (classnotfoundexception e) {         e.printstacktrace();     } catch (securityexception e) {         e.printstacktrace();     } catch (nosuchmethodexception e) {         e.printstacktrace();     } catch (illegalargumentexception e) {         e.printstacktrace();     } catch (instantiationexception e) {         e.printstacktrace();     } catch (illegalaccessexception e) {         e.printstacktrace();     } catch (invocationtargetexception e) {         e.printstacktrace();     } catch (nosuchfieldexception e) {         e.printstacktrace();     }      return orientation; } } 

then in main activity call this:

image.setimagebitmap(exifutils.rotatebitmap(path, decodesampledbitmap(new file(path), 400, 400)));   public bitmap decodesampledbitmap(file res, int reqwidth, int reqheight) {     if (res != null) {         // first decode injustdecodebounds=true check dimensions         final bitmapfactory.options options = new bitmapfactory.options();         options.injustdecodebounds = true;         try {             fileinputstream stream2 = new fileinputstream(res);              bitmapfactory.decodestream(stream2, null, options);              stream2.close();         } catch (filenotfoundexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }         // calculate insamplesize         bitmapfactory.options o2 = new bitmapfactory.options();         o2.insamplesize = calculateinsamplesize(options, reqwidth, reqheight);         o2.injustdecodebounds = false;         fileinputstream stream = null;         try {             stream = new fileinputstream(res);         } catch (filenotfoundexception e) {             e.printstacktrace();         }         bitmap bitmap = bitmapfactory.decodestream(stream, null, o2);         try {             stream.close();         } catch (ioexception e) {             e.printstacktrace();         }         return bitmap;     } else         return null; }  public int calculateinsamplesize(bitmapfactory.options options, int reqwidth, int reqheight) {     // raw height , width of image     final int height = options.outheight;     final int width = options.outwidth;     int insamplesize = 1;      if (height > reqheight || width > reqwidth) {          final int halfheight = height / 2;         final int halfwidth = width / 2;          // calculate largest insamplesize value power of 2 , keeps both         // height , width larger requested height , width.         while ((halfheight / insamplesize) > reqheight && (halfwidth / insamplesize) > reqwidth) {             insamplesize *= 2;         }     }      return insamplesize; } 

in code extract bitmap file path, give height , width , pass exifutils show bitmap in correct way.

i think wraps class , more.

if need anymore more ready offer


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -