android - Captured Image is not stored in a specified path? -


i making 1 app click images , save them in specified folder in external storage.

please follow code below.

capturesignature.java

package com.capturesignatureactivity;   import java.io.file; import java.io.fileoutputstream;  import java.util.calendar;   import android.app.activity;  import android.content.context;   import android.content.contextwrapper;   import android.content.intent;   import android.graphics.bitmap;   import android.graphics.canvas;   import android.graphics.color;    import android.graphics.paint;    import android.graphics.path;    import android.graphics.rectf;    import android.os.bundle;    import android.os.environment;    import android.provider.mediastore.images;    import android.util.attributeset;    import android.util.log;    import android.view.gravity;     import android.view.motionevent;    import android.view.view;   import android.view.view.onclicklistener;   import android.view.viewgroup.layoutparams;    import android.view.window;    import android.widget.button;    import android.widget.edittext;    import android.widget.linearlayout;   import android.widget.toast;     public class capturesignature extends activity {     linearlayout mcontent;    signature msignature;    button mclear, mgetsign, mcancel; public static string tempdir; public int count = 1; public string current = null; private bitmap mbitmap; view mview; file mypath;     private string uniqueid;    private edittext yourname;      @override     public void oncreate(bundle savedinstancestate)     {     super.oncreate(savedinstancestate);     this.requestwindowfeature(window.feature_no_title);     setcontentview(r.layout.signature);      tempdir = environment.getexternalstoragedirectory() + "/" + getresources().getstring(r.string.external_dir) + "/";     contextwrapper cw = new contextwrapper(getapplicationcontext());     file directory = cw.getdir(getresources().getstring(r.string.external_dir), context.mode_private);      preparedirectory();     uniqueid = gettodaysdate() + "_" + getcurrenttime() + "_" + math.random();     current = uniqueid + ".png";     mypath= new file(directory,current);       mcontent = (linearlayout) findviewbyid(r.id.linearlayout);     msignature = new signature(this, null);     msignature.setbackgroundcolor(color.white);     mcontent.addview(msignature, layoutparams.fill_parent, layoutparams.fill_parent);     mclear = (button)findviewbyid(r.id.clear);     mgetsign = (button)findviewbyid(r.id.getsign);     mgetsign.setenabled(false);     mcancel = (button)findviewbyid(r.id.cancel);     mview = mcontent;      yourname = (edittext) findviewbyid(r.id.yourname);      mclear.setonclicklistener(new onclicklistener()     {         public void onclick(view v)         {             log.v("log_tag", "panel cleared");             msignature.clear();             mgetsign.setenabled(false);         }     });      mgetsign.setonclicklistener(new onclicklistener()     {         public void onclick(view v)         {             log.v("log_tag", "panel saved");             boolean error = capturesignature();             if(!error){                 mview.setdrawingcacheenabled(true);                 msignature.save(mview);                 bundle b = new bundle();                 b.putstring("status", "done");                 intent intent = new intent();                 intent.putextras(b);                 setresult(result_ok,intent);                 finish();             }         }     });      mcancel.setonclicklistener(new onclicklistener()     {         public void onclick(view v)         {             log.v("log_tag", "panel canceled");             bundle b = new bundle();             b.putstring("status", "cancel");             intent intent = new intent();             intent.putextras(b);             setresult(result_ok,intent);             finish();         }     });      }       @override      protected void ondestroy() {     log.w("getsignature", "ondestory");     super.ondestroy();     }      private boolean capturesignature() {      boolean error = false;     string errormessage = "";       if(yourname.gettext().tostring().equalsignorecase("")){         errormessage = errormessage + "please enter name\n";         error = true;     }      if(error){         toast toast = toast.maketext(this, errormessage, toast.length_short);         toast.setgravity(gravity.top, 105, 50);         toast.show();     }      return error;     }      private string gettodaysdate() {      final calendar c = calendar.getinstance();     int todaysdate =     (c.get(calendar.year) * 10000) +             ((c.get(calendar.month) + 1) * 100) +             (c.get(calendar.day_of_month));     log.w("date:",string.valueof(todaysdate));     return(string.valueof(todaysdate));      }      private string getcurrenttime() {      final calendar c = calendar.getinstance();     int currenttime =     (c.get(calendar.hour_of_day) * 10000) +             (c.get(calendar.minute) * 100) +             (c.get(calendar.second));     log.w("time:",string.valueof(currenttime));     return(string.valueof(currenttime));      }       private boolean preparedirectory()    {     try     {         if (makedirs())         {             return true;         } else {             return false;         }     } catch (exception e)     {         e.printstacktrace();         toast.maketext(this, "could not initiate file system.. sdcard mounted properly?", 1000).show();         return false;     }    }     private boolean makedirs()    {     file tempdir = new file(tempdir);     if (!tempdir.exists())         tempdir.mkdirs();      if (tempdir.isdirectory())     {         file[] files = tempdir.listfiles();         (file file : files)         {             if (!file.delete())             {                 system.out.println("failed delete " + file);             }         }     }     return (tempdir.isdirectory());     }      public class signature extends view    {     private static final float stroke_width = 5f;     private static final float half_stroke_width = stroke_width / 2;     private paint paint = new paint();     private path path = new path();      private float lasttouchx;     private float lasttouchy;     private final rectf dirtyrect = new rectf();      public signature(context context, attributeset attrs)     {         super(context, attrs);         paint.setantialias(true);         paint.setcolor(color.black);         paint.setstyle(paint.style.stroke);         paint.setstrokejoin(paint.join.round);         paint.setstrokewidth(stroke_width);     }      public void save(view v)     {         log.v("log_tag", "width: " + v.getwidth());         log.v("log_tag", "height: " + v.getheight());         if(mbitmap == null)         {             mbitmap =  bitmap.createbitmap (mcontent.getwidth(), mcontent.getheight(),  bitmap.config.rgb_565);;         }         canvas canvas = new canvas(mbitmap);         try         {             fileoutputstream mfileoutstream = new fileoutputstream(mypath);              v.draw(canvas);             mbitmap.compress(bitmap.compressformat.png, 90, mfileoutstream);             mfileoutstream.flush();             mfileoutstream.close();             string url = images.media.insertimage(getcontentresolver(), mbitmap, "title", null);             log.v("log_tag","url: " + url);             //in case want delete file             //boolean deleted = mypath.delete();             //log.v("log_tag","deleted: " + mypath.tostring() + deleted);             //if want convert image string use base64 converter          }         catch(exception e)         {             log.v("log_tag", e.tostring());         }     }      public void clear()     {         path.reset();         invalidate();     }      @override     protected void ondraw(canvas canvas)     {         canvas.drawpath(path, paint);     }      @override     public boolean ontouchevent(motionevent event)     {         float eventx = event.getx();         float eventy = event.gety();         mgetsign.setenabled(true);          switch (event.getaction())         {             case motionevent.action_down:                 path.moveto(eventx, eventy);                 lasttouchx = eventx;                 lasttouchy = eventy;                 return true;              case motionevent.action_move:              case motionevent.action_up:                  resetdirtyrect(eventx, eventy);                 int historysize = event.gethistorysize();                 (int = 0; < historysize; i++)                 {                     float historicalx = event.gethistoricalx(i);                     float historicaly = event.gethistoricaly(i);                     expanddirtyrect(historicalx, historicaly);                     path.lineto(historicalx, historicaly);                 }                 path.lineto(eventx, eventy);                 break;              default:                 debug("ignored touch event: " + event.tostring());                 return false;         }          invalidate((int) (dirtyrect.left - half_stroke_width),                 (int) (dirtyrect.top - half_stroke_width),                 (int) (dirtyrect.right + half_stroke_width),                 (int) (dirtyrect.bottom + half_stroke_width));          lasttouchx = eventx;         lasttouchy = eventy;          return true;     }      private void debug(string string){     }      private void expanddirtyrect(float historicalx, float historicaly)     {         if (historicalx < dirtyrect.left)         {             dirtyrect.left = historicalx;         }         else if (historicalx > dirtyrect.right)         {             dirtyrect.right = historicalx;         }          if (historicaly < dirtyrect.top)         {             dirtyrect.top = historicaly;         }         else if (historicaly > dirtyrect.bottom)         {             dirtyrect.bottom = historicaly;         }     }      private void resetdirtyrect(float eventx, float eventy)     {         dirtyrect.left = math.min(lasttouchx, eventx);         dirtyrect.right = math.max(lasttouchx, eventx);         dirtyrect.top = math.min(lasttouchy, eventy);         dirtyrect.bottom = math.max(lasttouchy, eventy);     } } } 

my app runs images not stored in specified directory. stored in dcim folder.

please guide. thanx.

try this

file sd = android.os.environment.getexternalstoragedirectory(); file fichero = new file(sd, "signature.jpg"); 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -