android - captured image quality is low in gridview -


i creating android app in user can click images shown in grid view once image captured part works fine quality of image low , if take image high resolution camera shows width , height of image 160 , 200 px respectively. know there way capture image in original size compress image in whatsapp. since newbie android don't know have do.

public class camact extends activity implements onclicklistener { button capturebtn = null; final int camera_capture = 1; private uri picuri; private dateformat dateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); private gridview grid; private  list<string> listofimagespath;  public static final string gridviewdemo_imagepath = environment.getexternalstoragedirectory().getabsolutepath() + "/gridviewdemo/";  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.camlayout);      capturebtn = (button)findviewbyid(r.id.capture_btn1);     capturebtn.setonclicklistener(this);     grid = ( gridview) findviewbyid(r.id.gridviewimg);      listofimagespath = null;     listofimagespath = retrivecapturedimagepath();     if(listofimagespath!=null){         grid.setadapter(new imagelistadapter(this,listofimagespath));     } }   @override public void onclick(view arg0) {         if (arg0.getid() == r.id.capture_btn1) {          try {               intent captureintent = new      intent(mediastore.action_image_capture);             startactivityforresult(captureintent, camera_capture);         } catch(activitynotfoundexception anfe){             string errormessage = "whoops - device doesn't support capturing images!";             toast toast = toast.maketext(this, errormessage, toast.length_short);             toast.show();         }     } } protected void onactivityresult(int requestcode, int resultcode, intent data) {     if (resultcode == result_ok) {         if(requestcode == camera_capture){             bundle extras = data.getextras();             bitmap thepic = extras.getparcelable("data");             string imgcurtime = dateformat.format(new date());             file imagedirectory = new file(gridviewdemo_imagepath);             imagedirectory.mkdirs();             string _path = gridviewdemo_imagepath + imgcurtime+".jpg";             try {                 fileoutputstream out = new fileoutputstream(_path);                 thepic.compress(bitmap.compressformat.jpeg, 90, out);                 out.close();             } catch (filenotfoundexception e) {                 e.getmessage();             } catch (ioexception e) {                 e.printstacktrace();             }             listofimagespath = null;             listofimagespath = retrivecapturedimagepath();             if(listofimagespath!=null){                 grid.setadapter(new imagelistadapter(this,listofimagespath));             }         }     } }  private list<string> retrivecapturedimagepath() {     list<string> tfilelist = new arraylist<string>();     file f = new file(gridviewdemo_imagepath);     if (f.exists()) {         file[] files=f.listfiles();         arrays.sort(files);          for(int i=0; i<files.length; i++){             file file = files[i];             if(file.isdirectory())                 continue;             tfilelist.add(file.getpath());         }     }     return tfilelist; } 

imageadapter:

 public class imagelistadapter extends baseadapter {     private context context;     private list<string> imgpic;     public imagelistadapter(context c, list<string> thepic)     {         context = c;         imgpic = thepic;     }     public int getcount() {         if(imgpic != null)             return imgpic.size();         else             return 0;     }      //---returns id of item---     public object getitem(int position) {         return position;     }      public long getitemid(int position) {         return position;     }      //---returns imageview view---     public view getview(int position, view convertview, viewgroup parent)     {         imageview imageview;         bitmapfactory.options bfoptions=new bitmapfactory.options();         bfoptions.indither=false;                     //disable dithering mode         bfoptions.inpurgeable=true;                   //tell gc whether needs free memory, bitmap can cleared         bfoptions.ininputshareable=true;              //which kind of reference used recover bitmap data after being clear, when used in future         bfoptions.intempstorage=new byte[16 * 1024];         if (convertview == null) {             imageview = new imageview(context);             imageview.setlayoutparams(new gridview.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent));             imageview.setpadding(0, 0, 0, 0);         } else {             imageview = (imageview) convertview;         }         fileinputstream fs = null;         bitmap bm;         try {             fs = new fileinputstream(new file(imgpic.get(position).tostring()));              if(fs!=null) {                 bm=bitmapfactory.decodefiledescriptor(fs.getfd(), null, bfoptions);                 imageview.setimagebitmap(bm);                 imageview.setid(position);                 imageview.setlayoutparams(new gridview.layoutparams(200, 160));             }         } catch (ioexception e) {             e.printstacktrace();         } finally{             if(fs!=null) {                 try {                     fs.close();                 } catch (ioexception e) {                     e.printstacktrace();                 }             }         }         return imageview;     } } 

try this:

  capturebtn.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             if(subname.gettext().tostring().isempty()){                 snackbar.make(view, "select subject", snackbar.length_long).show();              }else {                 intent intent = new intent(                         android.provider.mediastore.action_image_capture);                 imagefileuri = uri.fromfile(getfile());                  intent.putextra(mediastore.extra_output, imagefileuri);                 startactivityforresult(intent, camera_pic_request);             }         }     });  protected void onactivityresult(int requestcode, int resultcode, intent data) {     if (resultcode == result_ok) {          if(requestcode == camera_pic_request){             display display = getwindowmanager().getdefaultdisplay();             point size = new point();             display.getsize(size);             int dw = size.x;             int dh = size.y;             // load image's dimensions not image             bitmapfactory.options bmpfactoryoptions = new bitmapfactory.options();             bmpfactoryoptions.injustdecodebounds = true;             bitmap bmp = bitmapfactory.decodefile(imagefileuri.getpath(),                     bmpfactoryoptions);             int heightratio = (int) math.ceil(bmpfactoryoptions.outheight                     / (float) dh);             int widthratio = (int) math.ceil(bmpfactoryoptions.outwidth                     / (float) dw);             log.v("heightratio", "" + heightratio);             log.v("widthratio", "" + widthratio);             if (heightratio > 1 && widthratio > 1) {                 if (heightratio > widthratio) {                     // height ratio larger, scale according                     bmpfactoryoptions.insamplesize = heightratio;                 } else {                     // width ratio larger, scale according                     bmpfactoryoptions.insamplesize = widthratio;                 }             }             // decode real             bmpfactoryoptions.injustdecodebounds = false;             bmp = bitmapfactory.decodefile(imagefileuri.getpath(),                     bmpfactoryoptions);             initilizegridlayout();              // loading image paths sd card             imagepaths = utils.getfilepaths();              // gridview adapter             adapter = new gridviewimageadapter(cam.this, imagepaths,                     columnwidth);              // setting grid view adapter             gridview.setadapter(adapter);          }      }else if (resultcode == result_canceled) {         // user cancelled image capture         toast.maketext(getapplicationcontext(),                 "user cancelled image capture", toast.length_short).show();     } else {         // failed capture image         toast.maketext(getapplicationcontext(),                 "sorry! failed capture image", toast.length_short)                 .show();     } } 

it capture image per camera width , height image in quality image adapter using has temp storage can store 5 or 6 images of high quality change grid view .to know more displaying images sdcard gridview use following link: loading images sdcard , display in gridview

hope code helps you:)


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -