java - screenshot saving as autogenerated file name -


i made button take screenshot , save pictures folder. set being saved under name capture.jpeg wanted saved such cafe001.jpeg, cafe002.jpeg this. please let me know how can save time format.jpeg ? thank in advance

container = (linearlayout) findviewbyid(r.id.linearlayout1);         button capturebutton = (button) findviewbyid(r.id.capturebutton);         capturebutton.setonclicklistener(new onclicklistener() {         public void onclick(view v) {             container.builddrawingcache();             bitmap captureview = container.getdrawingcache();             fileoutputstream fos;             try {                 fos = new fileoutputstream(environment.getexternalstoragepublicdirectory(environment.directory_pictures).tostring() + "capture.jpeg");                 captureview.compress(bitmap.compressformat.jpeg, 100, fos);             } catch (filenotfoundexception e) {                 e.printstacktrace();             }             toast.maketext(getapplicationcontext(),                     "captured under pictures drectory", toast.length_long)                     .show();         }     }); 

you have couple of choices...

you could...

list files in directory , simple increment file count 1 , use that...

file[] files = new file(environment.getexternalstoragepublicdirectory(environment.directory_pictures).tostring()).     listfiles(new filefilter() {         public boolean accept(file pathname) {             string name = pathname.getname();             return pathname.isfile() && name.tolowercase().startswith("capture") && name.tolowercase().endswith(".jpeg");         } });  int filecount = files.length();  fos = new fileoutputstream(environment.getexternalstoragepublicdirectory(environment.directory_pictures).tostring() +      "capture" + filecount + ".jpeg"); 

this, of course, doesn't take account if file same index exists...

you could...

list files, sort them, grab last one, find it's index value , increment that...

something like...

file[] files = new file(environment.getexternalstoragepublicdirectory(environment.directory_pictures).tostring()).     listfiles(new filefilter() {         public boolean accept(file pathname) {             string name = pathname.getname();             return pathname.isfile() && name.tolowercase().startswith("capture") && name.tolowercase().endswith(".jpeg");         } });  arrays.sort(files); file last = files[files.length - 1];  pattern pattern = pattern.compile("[0-9]+"); matcher matcher = pattern.matcher(last.getname());  int index = 1; if (matcher.find()) {     string match = matcher.group();     index = integer.parseint(match) + 1; }  string filename = "capture" + index + ".jpeg" 

you could...

simply create loop , loop until find empty index position...for example see, java autogenerate directories if exists


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -