0
votes

So I am aware that there is SD Card access API which allows us to write files via DocumentProvider and DocumentFiles. I have made it work on removable SD Cards. I was always confused about External and Internal Storage. I always thought External Storage is Always SD card but today I came to know that it is not so.

So I have three question .

Question1 , how to know if Files are stored in external emulated storage or sdcard ? One solution maybe by searching for instances of "sdcard0" or "emulated" in the file path. Will this solution always work? I mean on all phones?

Question 2 what to Use for writing files on emulated storage(non removable external storage) normal files or DocumentFile?

Question 3 If solution of Q2 is Document File then why doesn't this work ?

private static String[] getExtSdCardPaths() { List paths = new ArrayList<>();

for (File file : GlobalSongList.GetInstance().getApplicationContext().getExternalFilesDirs("external")) {

    if (file != null && !file.equals(GlobalSongList.GetInstance().getApplicationContext().getExternalFilesDir("external"))) {
        int index = file.getAbsolutePath().lastIndexOf("/Android/data");
        if (index < 0) {
            Log.w("StorageAccessAPI", "Unexpected external file dir: " + file.getAbsolutePath());
        }
        else {
            String path = file.getAbsolutePath().substring(0, index);
            try {
                path = new File(path).getCanonicalPath();
            }
            catch (IOException e) {
                // Keep non-canonical path.
            }
            paths.add(path);
        }
    }
}
return paths.toArray(new String[paths.size()]);

}

1

1 Answers

0
votes

So I am aware that there is SD Card access API which allows us to write files via DocumentProvider and DocumentFiles.

You are referring to the Storage Access Framework. This allows you to read and write streams, not files, where the streams are backed by document providers. The user chooses what document provider to use, which in turn determines where the stream's content is stored. That could be local (e.g., external storage, removable storage) or remote (Google Drive, Dropbox, Samba file server, Web server, FTP server, SFTP server, etc.).

how to know if Files are stored in external emulated storage or sdcard ?

If you are using the Storage Access Framework, you do not know where the stream's content is stored.

what to Use for writing files on emulated storage(non removable external storage) normal files or DocumentFile?

If you explicitly want to use external storage, use external storage.

Using the Storage Access Framework allows the user to choose where the stream's content is stored, which may or may not be external storage.

why doesn't this work ?

I have no idea what you expect that to do. I expect it to return a series of useless strings.