I am experiencing some problems when I try to retrieve a saved picture on the sd-card.

Basically, the code that I am using when an image is taken is:

```
    if(now==null){

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
            now = sdf.format(new Date());
        }

        myDirVids=new File(Environment.getExternalStorageDirectory()+"/TimelapseVideos");
        if(!myDirVids.exists()){
            myDirVids.mkdirs();
        }
        myDirPhotos=new File(Environment.getExternalStorageDirectory()+"/TimelapsePhotos");
        if(!myDirPhotos.exists()){
            myDirPhotos.mkdirs();
        }

        myDir=new File(myDirPhotos+"/timelapse"+now);
        myDir.mkdirs();
        DecimalFormat decimalFormat = new DecimalFormat("00000");
        imagename = "Image-"+ decimalFormat.format(num) +".jpg";
        file = new File (myDir, imagename);
        if (file.exists ()) file.delete (); 
        try {
            FileOutputStream fos = new FileOutputStream(file);

            fos.write(data);
            fos.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

```

Just after this lines, I try to retrieve the image saved and I am getting a NullPointerException because it seems that is not available:

```
    Log.d(TAG, file.getAbsolutePath());
    Log.d(TAG, imagename);

try { 
Thread.sleep(800); 
} catch (InterruptedException e) {              
e.printStackTrace();
}

```

I get the nullPointerException on the first line, `Log.d(TAG, file.getAbsolutePath());` 

But if I put this line AFTER the sleep, no nullPointerException is thrown.
The sleep has to be superior to 500ms to not getting any error.

I don't know where the problem comes from, but I believe that when the FileOutPutStream operation finishes the smartphone is still saving the data in the sd-card. If I let the phone some time (0.5-1s) the problem disappear because the writing process has finalized. 

My question is: 

There is any way to know when a writing operation finishes?