Get external storage path(sd card)

plugins
android

#1

This maybe a special case due to my device speciality.
Anyone know how to read/write to sd card if I already know the external path as /storage/sdcard?
Below line,

let sdCardPath = fs.path.join(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM).toString());

Only will return the device internal storage.

When I tried to ‘hard code’ the folder path, I can see the list by run

let sdPath: fs.Folder = fs.Folder.fromPath("/storage/sdcard1");
            sdPath.getEntities().then(function (entities) {
                entities.forEach(function (entity) {
                    if (entity.name == "DCIM") {
                        var folder: fs.Folder = fs.Folder.fromPath(entity.path);
                        var testFile: fs.File = folder.getFile("test.txt");
                        try {
                            testFile.writeText("something")
                                .then(function () {
                                    console.log("aa " + folder.path);
                                    testFile.readText()
                                        .then(function (content) {
                                            console.log(content.toString());  
                                        }, function (error) {
                                            console.log("Fail to read" + error);
                                        });
                                }, function (error) {
                                    console.log("Fail to write " + error);
                                });
                        } catch (e) {
                            console.log("Save error " + e);
                        }
                    }
                });
            }, function (error) {
               
            });
            } catch (e) {
                console.error(e);
            }

I can only see all entities name, lastModified… but no write/read allowed.
Error:

Fail to write Error: java.io.FileNotFoundException: /storage/sdcard1/DCIM/test.txt: open failed: EACCES (Permission denied)```
I already set up external permissions in AndroidManifest.xml. 
And if I can access to sd card path by running android terminal command.

#2

External storage necessarily need not to be SD card, in most devices today the external storage is part of built-in flash drive.

You may try using getExternalFilesDirs, loop through available paths to find your desired storage, you may check whether its removable to make sure its a SD card.


#3

Actually, I can loop it and find sd card path. But it gave me the EACCES (Permission denied) . Any idea?


#4

Are you still using hard coded path? Which device & OS version you are testing it against?


#5

Hi @manojdcoder, I am using android 5.1.1.
getExternalFilesDirs will print out what the manufacturer set up ad “External”.
Unfortunately, getExternalFilesDirs return is internal device storage not sd card.
The way I got that ‘hardcode’ path is to run bash command in terminal.
for those who may have similar issues,@devyaz provided a solution here.


#6

Are you using getExternalFilesDir or getExternalFilesDirs. The second one is going to return array of paths, usually 2nd or later index will be your removable storage.


#7

Neither of them return the sdcard path.
I got what the sdcard path by run adb shell in android terminal.


#8

Thanks @manojdcoder for his help,
For everyone which may have the similar problem as me,
Here is what I did for android device

     let deviceInternalPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM).getAbsolutePath();
     let externalath = <any>android.support.v4.content.ContextCompat;
        try {
           
            let storagePaths: any = externalath.getExternalFilesDirs(applicationModule.android.currentContext, null);
      
            sdcardPath = storagePaths[1].getAbsolutePath().toString();
        }
        catch (e) {
            console.log("Context compat " + e)
            
        }

By using ContextCompat.getExternalStoragePublicDirectory(), you can find all external storage. For android 5.1.1, there would be three return values, 1st is device internal, 2nd is sd card, 3rd one is encrypted sd card.