0

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()]);

}

Ankit Srivastava
  • 853
  • 1
  • 9
  • 28

1 Answers1

0

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So here is the use Case, With My app I want to edit tags of the files So files have paths, right ? now you can actually create a documentFile out of that file as explained here , check out the second answer. Now it doesn't work for emulated storage which I am trying to solve by asking these questions – Ankit Srivastava Feb 11 '16 at 16:30
  • Here is the link http://stackoverflow.com/questions/26744842/how-to-use-the-new-sd-card-access-api-presented-for-lollipop – Ankit Srivastava Feb 11 '16 at 16:30
  • Also what do you mean by this "If you explicitly want to use external storage, use external storage" – Ankit Srivastava Feb 11 '16 at 16:33
  • @AnkitSrivastava: "My app I want to edit tags of the files So files have paths, right ?" -- the Storage Access Framework does not deal with files. "now you can actually create a documentFile out of that file as explained here , check out the second answer" -- I do not expect that code to be reliable across devices and Android OS versions, or even across users. "If you explicitly want to use external storage, use external storage" -- if you want to work with files, stop using the Storage Access Framework, plain and simple. – CommonsWare Feb 11 '16 at 16:51
  • how do you think I will write files on lollipop or above then ? – Ankit Srivastava Feb 11 '16 at 16:53
  • @AnkitSrivastava: If you want to work with files, use [internal storage](https://commonsware.com/blog/2014/04/07/storage-situation-internal-storage.html) and [external storage](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html), the way we have in Android since 2008. – CommonsWare Feb 11 '16 at 16:55
  • You can't in lollipop and above, that's what I trying to say, It has been over an year,don't you know that already ? – Ankit Srivastava Feb 11 '16 at 16:56
  • @AnkitSrivastava: [Internal storage](https://commonsware.com/blog/2014/04/07/storage-situation-internal-storage.html) and [external storage](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html) have not changed significantly since 2008. [Removable storage](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html) changed in Android 4.4, to grant limited access and block access outside of that. Internal storage, external storage, and removable storage are three different things. – CommonsWare Feb 11 '16 at 16:59
  • If you read my questions again ,you will now understand what I am trying to ask, your answers were totally different to what I asked . I clearly mentioned File and DocumentFile separately , Anyways , I got my answer – Ankit Srivastava Feb 11 '16 at 17:05