Android: Creating file using createNewFile() method

Somehow createNewFile() was not able to create the complete file path here on my devices.

try {
    if (!futurePhotoFile.exists()) {
        new File(futurePhotoFile.getParent()).mkdirs();
        futurePhotoFile.createNewFile();
    }
} catch (IOException e) {
    Log.e("", "Could not create file.", e);
    Crouton.showText(TaskDetailsActivity.this,
            R.string.msgErrorNoSdCardAvailable, Style.ALERT);
    return;
}

CreateNewFile() is used like this:

File file = new File("data/data/your package name/test.txt");
if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

So you will tell the file where it should be created. Remember that you can only create new files on your package. That is "data/data/your package name/".

Tags:

Android