Skip to content Skip to sidebar Skip to footer

How To Save Files In External Storage Public Directory Documents On Android 4.1.2

i want to save a file on my Android 4.1.2 smartphone in the documents directory. This code snippet: File file = new File(Environment.getExternalStoragePublicDirectory(Environment.

Solution 1:

You are not able to access DIRECTORY_DOCUMENTS becuase it is not there in Android 4.1.2. Which means, though there is a Documents directory in your external storage, it is not pointed to by DIRECTORY_DOCUMENTS (since it is non-existent). To solve this, you have to create the directory if it is not present and get the path to that folder manually.

FiledocsFolder=newFile(Environment.getExternalStorageDirectory() + "/Documents");
booleanisPresent=true;
if (!docsFolder.exists()) {
    isPresent = docsFolder.mkdir();
}
if (isPresent) {
    Filefile=newFile(docsFolder.getAbsolutePath(),"test.txt"); 
} else {
    // Failure
}

Solution 2:

The folder is present in older Versions (from Api 1) of Android but the field DIRECTORY_DOCUMENTS is available first in Version 4.4 (Api 19 - Kitkat).

Solution for me was to use Environment.getExternalStorageDirectory() + "/Documents" instead DIRECTORY_DOCUMENTS for older Android Versions.

Post a Comment for "How To Save Files In External Storage Public Directory Documents On Android 4.1.2"