java - how can i overwrite a file if it exists in directory -


i'm saving image in directory same name (for purpose) when file exists there won't overwritten , how know ? because when i'm saving file existing file not changing when use different name file works. want replace existing file new one. far i'm trying :

 content.setdrawingcacheenabled(true);                 bitmap bitmap = content.getdrawingcache(); // bitmap file saving                   file file = new file(context.getapplicationcontext().getfilesdir() + "/img.jpg"); //here's path i'm saving file                  if (file.exists()){                      file.delete(); // here i'm checking if file exists , if yes i'm deleting not working                  }                   string path = file.getpath();                  try {                     file.createnewfile();                     fileoutputstream ostream = new fileoutputstream(file);                     bitmap.compress(bitmap.compressformat.jpeg, 60, ostream);                       savedfile = new file(path);                       ostream.close();                 }                 catch (exception e)                 {                     e.printstacktrace();                  } 

i'm doing check right after deleting existing file :

                if (file.exists()){                      toast.maketext(context , "still exists",toast.length_short).show();                 } 

and time file not here cause toast not appearing.

first, let insert permission in androidmanifest.xml file (less android 6.0 version)

android:name="android.permission.write_internal_storage" /> 

then, use below code write bitmap file

string path = environment.getexternalstoragedirectory().tostring(); outputstream fout = null; file file = new file(path, "fitnessgirl"+".jpg"); // file save fout = new fileoutputstream(file); bitmap picturebitmap = content.getdrawingcache();// bitmap //bitmap picturebitmap = getimagebitmap(myurl); // obtaining bitmap picturebitmap.compress(bitmap.compressformat.jpeg, 85, fout); // saving bitmap file compressed jpeg 85% compression rate fout.flush(); fout.close(); // not forget close stream  mediastore.images.media.insertimage(getcontentresolver(),file.getabsolutepath(),file.getname(),file.getname()); 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -