How to save files in External Storage Public Directory DOCUMENTS on Android 4.1.2

大兔子大兔子 提交于 2019-12-03 05:12:31
Arjun

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.

File docsFolder = new File(Environment.getExternalStorageDirectory() + "/Documents");
boolean isPresent = true;
if (!docsFolder.exists()) {
    isPresent = docsFolder.mkdir();
}
if (isPresent) {
    File file = new File(docsFolder.getAbsolutePath(),"test.txt"); 
} else {
    // Failure
}
iwanlenin

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!