11

i'm saving image in my directory with same name (for a purpose) but when file is already exists there it won't get overwritten , how do i know that ? because when i'm saving this file the already existing file is not changing but when i use a different name for my file it works. so what i want is to replace the existing file with a new one. so far this is what i'm trying :

 content.setDrawingCacheEnabled(true);
                Bitmap bitmap = content.getDrawingCache(); // an bitmap file for saving


                File file = new File(context.getApplicationContext().getFilesDir() + "/img.jpg"); //here's the path where i'm saving my file

                if (file.exists()){

                    file.delete(); // here i'm checking if file exists and if yes then i'm deleting it but its 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 another check right after deleting existing file :

                if (file.exists()){

                    Toast.makeText(context , "Still EXISTS",Toast.LENGTH_SHORT).show();
                }

and this time file is not here cause the toast is not appearing.

remy boys
  • 2,928
  • 5
  • 36
  • 65

3 Answers3

2

Add this line to Your Code

FileWriter fw = new FileWriter(myDir + "/score.jpg", false);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
sai
  • 126
  • 1
  • 8
2

First, Let insert the permission in your AndroidManifest.xml file (less than Android 6.0 version)

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

Then, use the below code to write the bitmap to file

String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "FitnessGirl"+".jpg"); // the File to save to
fOut = new FileOutputStream(file);
Bitmap pictureBitmap = content.getDrawingCache();// Your bitmap
//Bitmap pictureBitmap = getImageBitmap(myurl); // obtaining the Bitmap
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush();
fOut.close(); // do not forget to close the stream

MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
Jame
  • 3,746
  • 6
  • 52
  • 101
  • Just part file. You can delete it. I updated it. Accept it if it is useful – Jame Aug 29 '16 at 05:42
  • hey man sorry for responding late , was at work so i tried your code but still effect is same. see my code – remy boys Aug 29 '16 at 08:44
  • 1
    `String path = CropActivity.this.getApplicationContext().getFilesDir().toString(); OutputStream fOut = null; File file = new File(path, "img"+".jpg"); savedFile = file; try { fOut = new FileOutputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); }` – remy boys Aug 29 '16 at 08:45
  • `Bitmap pictureBitmap = content.getDrawingCache(); pictureBitmap = getImageBitmap(myurl); bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); fOut.flush(); try { fOut.close(); } catch (IOException e) { e.printStackTrace(); }` – remy boys Aug 29 '16 at 08:46
  • `try { MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName()); } catch (FileNotFoundException e) { e.printStackTrace(); } Log.d("saved",""+savedFile.toString());` – remy boys Aug 29 '16 at 08:47
  • the file is being saved at the same place but still no change in the file. – remy boys Aug 29 '16 at 08:47
  • I think first you have to delete the file first. Ref :http://stackoverflow.com/questions/5486529/delete-file-from-internal-storage – Jame Aug 29 '16 at 09:02
  • this is how i'm deleting file : `File alreadyStoredFile = new File(CropActivity.this.getApplicationContext().getFilesDir() + "/img"+".jpg"); Log.d("TG",""+alreadyStoredFile); alreadyStoredFile.deleteOnExit(); Log.d("bool",""+alreadyStoredFile.exists());` – remy boys Aug 29 '16 at 09:13
  • and after that i'm doing the saving thing.. , but still same behavior exists. – remy boys Aug 29 '16 at 09:14
  • OK. Let add one more permission `` . Then try to make a new image, if it is successful, then call delete function. If the step is also successful, then finally you can call delete+create new image – Jame Aug 29 '16 at 09:44
  • **FALSE AND MISLEADING** - there is no such permission as android.permission.WRITE_INTERNAL_STORAGE – Chris Stratton Jan 24 '18 at 05:51
1

Try this,

boolean result = Files.deleteIfExists(file.toPath()); 

Also make sure you have write permission enabled!

PS: From Android Lollipop you need the runtime permission and the permission already set in manifest file won't work.

Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50