1

I have a question about this code I found on Android saving file to external storage

Before rushing to down vote, I'm asking this here because I'm not allowed to comment on answered questions.

    private void saveImageToExternalStorage(Bitmap finalBitmap) {
    String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    File myDir = new File(root + "/saved_images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-" + n + ".jpg";
    //////////////////////////////////////////////////////////////////////////////////

    File file = new File(myDir, fname);    
    if (file.exists())                        // why check if it exists and delete file??????????                       
        file.delete();

    try {
       FileOutputStream out = new FileOutputStream(file);             // how is it being used if it is deleted???
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();
     }
         catch (Exception e) {
         e.printStackTrace();
    }


    // Tell the media scanner about the new file so that it is
    // immediately available to the user.
   MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null,
            new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    Log.i("ExternalStorage", "Scanned " + path + ":");
                    Log.i("ExternalStorage", "-> uri=" + uri);
                }
    });

}

my question is about the

if (file.exists())       

isn't this always executed after the statement above is executed? also why delete it after creating it, Does the statement only delete the contents and not the actual file. I read the Java doc's but it didn't clarify this for me.

thank you.

Community
  • 1
  • 1
Ammar Samater
  • 529
  • 2
  • 7
  • 24
  • Creating a new instance of the `File` class doesn't really create a file on the file system that is why `exists()` returns `true` only if the file already exists. – Titus Sep 01 '15 at 16:11
  • I see, so the file object is really just a data structure that holds the path to a memory location? – Ammar Samater Sep 01 '15 at 16:22
  • yes, and the file will be created (on the file system) as soon as you try to write something to it. – Titus Sep 01 '15 at 16:26

1 Answers1

3

isn't this always executed after the statement above is executed?

Well, yes.

also why delete it after creating it

new File(...) does not create a file on disk. It creates a File object in the Java programming language.

Does the statement only delete the contents and not the actual file

delete() deletes the file.

I read the Java doc's but it didn't clarify this for me.

Quoting the documentation: "The actual file referenced by a File may or may not exist. It may also, despite the name File, be a directory or other non-regular file."

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I understand now I think. So FileOutputStream out = new FileOutputStream(file); creates the file and the following statement writes the image to the file ? – Ammar Samater Sep 01 '15 at 16:24
  • @AmmarSamater: "So FileOutputStream out = new FileOutputStream(file); creates the file" -- um, probably. I have not experimented with creating a `FileOutputStream`, not writing anything to it, then closing it and seeing what the results are. It might optimize to only create the file on the first call that actually writes bytes to it. "the following statement writes the image to the file ?" -- yes, converting the `Bitmap` to JPEG along the way. – CommonsWare Sep 01 '15 at 16:38