java - How can I delete a pre-existing image from storage before re-downloading using DownloadManager? -


i writing code android app using eclipse supposed download image url (which generated app, elsewhere in code, using gps information), attach newly downloaded image e-mail sent. able to, in general, accomplish without issue.

problem this: want 1 image downloaded app present in device's external storage @ given time. deleting image after email intent not work, because because app doesn't call onstop or ondestroy when switching app send email. time-sensitive deleting of image not work either, because cannot assume user send 1 email app per hour. want give user freedom of sending many of these emails (with 1 newly downloaded image, each) wish.

current method (which works of time) this: in downloadfile method, check file's existence (i call sensormap.png), delete if exists, before downloading new one. should ensure there may 1 sensormap.png image in external storage @ given time (edit: this), , when comes time attach image email intent, there 1 image ready go. instead, i see second sensormap image being downloaded storage (i.e. "sensormap-1.png"), or image cannot attached email due "file size: 0 bytes" error, or image cannot attached due "file not exist" error. unsure difference between latter 2 problems is. edit: upon manually examining contents of directory created, seems that, intended, end 1 image titled "sensormap.png" @ time; remains in directory after app closes, expected. however, still "file size: 0 bytes" message or "file not exist." message no attached image, though see image exist upon looking in directory afterwards. other times, works fine. it's rather bewildering.

in addition, there issue of button sends email becoming unresponsive occasionally. of time, prompts user select email client, intended, button if clicked, nothing. when happens, logcat not sense button clicked (i inserted println statement test it). unsure of why delete-before-download not working flawlessly; basic idea, @ least, appears logically sound. here code pertaining issue:

code used download file (in maincountactivity.java):

//function download image given url. use attach image file email.         public void downloadfile(string url) {              //delete existing file first 1 sensormap image exists in memory              //at given time.              file file = new file(environment.getexternalstoragedirectory()+"/sensorlocationimages");             file checkfile = new file(environment.getexternalstoragedirectory()+"/sensorlocationimages/sensormap.png");             if(checkfile.exists())              {                 //debugging:                 system.out.println("about delete file!");                  //deletefiles(environment.getexternalstoragedirectory()+"/sensorlocationimages");                 checkfile.delete();             }              downloadmanager mgr = (downloadmanager) getactivity().getsystemservice(context.download_service);              uri downloaduri = uri.parse(url);             downloadmanager.request request = new downloadmanager.request(                     downloaduri);              request.setallowednetworktypes(                     downloadmanager.request.network_wifi                             | downloadmanager.request.network_mobile)                     .setallowedoverroaming(false).settitle("sensor location map")                     .setdescription("pinpointed location log file sent.")                     .setdestinationinexternalpublicdir("/sensorlocationimages", "sensormap.png");              mgr.enqueue(request);          }          public activity getactivity() //i wasn't sure if work, did. or @ least appears to.         { return this; } 



method send email (in maincountactivity.java):

public void sendemail(string toaddress, string ccaddress, string bccaddress, string subject, string body, string attachmentmimetype) throws exception{             try {                 intent emailintent = new intent(intent.action_send_multiple);                 emailintent.settype(attachmentmimetype); //new                 string stoaddress[] = { toaddress };                   string sccaddress[] = { ccaddress};                   string sbccaddress[] = { bccaddress };                   emailintent.setflags(intent.flag_activity_new_task);                 emailintent.putextra(intent.extra_email, stoaddress);                 emailintent.putextra(android.content.intent.extra_cc, sccaddress);                   emailintent.putextra(android.content.intent.extra_bcc, sbccaddress);                  emailintent.putextra(intent.extra_subject, subject);                 emailintent.putextra(intent.extra_text, body);                  //get uri of logfile                 file tempfile = new file(environment.getexternalstoragedirectory () + maincountactivity.dirpath);                 uri uri = uri.fromfile(tempfile);                  //create uri arraylist , add first uri                 arraylist<uri> uris = new arraylist<uri>();                 uris.add(uri);                  //get uri of map image , add arraylist                 //make sure there attach                 file file = new file(environment.getexternalstoragedirectory()+"/sensorlocationimages");                 {                     downloadfile(getmaplink());                     //createdirectoryandsavefile(getbitmapfromurl(getmaplink()), "sensormap.png");                 } while (!file.exists());                  uris.add(uri.fromfile(new file(environment                         .getexternalstoragedirectory()                         + "/sensorlocationimages/sensormap.png")));                         //+ "/sdcard/sensorlocationimages/sensormap.png")));                  emailintent.putparcelablearraylistextra(intent.extra_stream, uris);                  startactivity(emailintent);             }             catch(exception ex) {                 ex.printstacktrace();                 throw ex;             }         } 



onclick method, occasional button issue (in maincountactivity.java):

public void onclick(view v){                  switch(v.getid())                 {                     case r.id.textview1:                     {                         break;                     }                     case r.id.reset:                     {                         //allowcounting let program know when let count or not, depending if start or stop button pressed.                         logcount=0;                         mcounter.settext("total: 0");                         mtoggle.setchecked(false);                          break;                     }                    /* case r.id.togglebutton:                     {                         break;                     }*/                     case r.id.sendemail:                     {                         //for debugging purposes:                         system.out.println("email button being clicked!");                          locationmanager locationmanager = (locationmanager) getsystemservice(location_service);                         if (locationmanager.isproviderenabled(locationmanager.gps_provider))                             {                               toast.maketext(this, "gps enabled in device", toast.length_short).show();                               try {                                     sendemail("","","","sensor log info",getemailbody(),"multipart/mixed");                                 } catch (exception e) {                                     e.printstacktrace();                                 }                             }                         else                             {                               showgpsalertforemail();                             }                          break;                     }                  } 



basically, want know why delete-then-download method has not worked every time. logcat errors have provided no insight. thank time.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -