java - Camera app saves empty files on back button pressed -


i using camera app in fragment. works fine , taken pictures saved , wish.

the problem when in photo app , press button on device, empty image gets saved. how can avoid that?

here cameraactivity:

public class cameraactivity extends activity {     private static final string tag = "cameraactivity";     string currentfilepath;     string currentfilename;      @override     protected void oncreate(@nullable bundle savedinstancestate) {         super.oncreate(savedinstancestate);          new cameratask().execute();     }      public void dispatchpictureintent() {         intent takepictureintent = new intent(mediastore.action_image_capture);         if (takepictureintent.resolveactivity(getpackagemanager()) != null) {              file photofile = null;             try {                 photofile = createimagefile();             } catch (ioexception e) {                 toast.maketext(this, r.string.msg_picture_not_saved, toast.length_long).show();             }             if (photofile != null) {                 uri photouri = fileprovider.geturiforfile(this, getclass().getcanonicalname(), photofile);                 takepictureintent.putextra(mediastore.extra_output, photouri);                 startactivityforresult(takepictureintent, constants.request_to_take_picture);                  addpicturetogallery();                 dispatchdataintent();             }         }     }      public file createimagefile() throws ioexception {         file pathofstoragedir = environment.getexternalstoragepublicdirectory(environment.directory_dcim + "/camera");         pathofstoragedir.mkdir();          string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date());         string fileprefix = "img_" + timestamp + "_";         string suffix = ".jpg";          file image = file.createtempfile(fileprefix, suffix, pathofstoragedir);         currentfilename = image.getname();         currentfilepath = image.getabsolutepath();         return image;     }      private void addpicturetogallery() {         intent mediascanintent = new intent(intent.action_media_scanner_scan_file);         file f = new file(currentfilepath);         uri contenturi = uri.fromfile(f);         mediascanintent.setdata(contenturi);         this.sendbroadcast(mediascanintent);     }      private void dispatchdataintent() {         intent data = new intent();         data.putextra(constants.image_file_name, currentfilename);         setresult(cameraactivity.result_ok, data);     }      private class cameratask extends asynctask<void, void, void> {          @override         protected void doinbackground(void... params) {             dispatchpictureintent();             return null;         }          @override         protected void onpostexecute(void avoid) {             super.onpostexecute(avoid);             finish();         }     } } 

thanks help!

your code save image file. try delete saved picture if user pressed on button. better way save image in onactivityresult, after check result code.

public class cameraactivity extends activity {     private static final string tag = "cameraactivity";     string currentfilepath;     string currentfilename;      @override     protected void oncreate(@nullable bundle savedinstancestate) {         super.oncreate(savedinstancestate);          new cameratask().execute();     }      public void dispatchpictureintent() {         intent takepictureintent = new intent(mediastore.action_image_capture);         if (takepictureintent.resolveactivity(getpackagemanager()) != null) {              file photofile = null;             try {                 photofile = createimagefile();             } catch (ioexception e) {                 toast.maketext(this, r.string.msg_picture_not_saved, toast.length_long).show();             }             if (photofile != null) {                 uri photouri = fileprovider.geturiforfile(this, getclass().getcanonicalname(), photofile);                 takepictureintent.putextra(mediastore.extra_output, photouri);                 startactivityforresult(takepictureintent, constants.request_to_take_picture);              }         }     }      public file createimagefile() throws ioexception {         file pathofstoragedir = environment.getexternalstoragepublicdirectory(environment.directory_dcim + "/camera");         pathofstoragedir.mkdir();          string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date());         string fileprefix = "img_" + timestamp + "_";         string suffix = ".jpg";          file image = file.createtempfile(fileprefix, suffix, pathofstoragedir);         currentfilename = image.getname();         currentfilepath = image.getabsolutepath();         return image;     }      private void addpicturetogallery() {         intent mediascanintent = new intent(intent.action_media_scanner_scan_file);         file f = new file(currentfilepath);         uri contenturi = uri.fromfile(f);         mediascanintent.setdata(contenturi);         this.sendbroadcast(mediascanintent);     }      private void dispatchdataintent() {         intent data = new intent();         data.putextra(constants.image_file_name, currentfilename);         setresult(cameraactivity.result_ok, data);     }      private class cameratask extends asynctask<void, void, void> {          @override         protected void doinbackground(void... params) {             dispatchpictureintent();             return null;         }          @override         protected void onpostexecute(void avoid) {             super.onpostexecute(avoid);         }     }      protected void onactivityresult(int requestcode, int resultcode, intent data) {         if (requestcode == constants.request_to_take_picture){             if(resultcode == result_ok) {                  addpicturetogallery();                 dispatchdataintent();             } else {                 file f = new file(currentfilepath);                 f.delete();             }         }             finish();      } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -