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

后端 未结 2 499
天命终不由人
天命终不由人 2021-02-07 12:13

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.getExternalStorageP         


        
2条回答
  •  情话喂你
    2021-02-07 12:21

    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
    }
    

提交回复
热议问题