How do i create a Folder in external storage

nativescriptrocks

#1

i am trying to create a folder in external storage to hold assets from server and also to have Sqlite db file in external storage, this is how am doing it

var folderPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOCUMENTS).toString();
var folder=fsModule.path.join(folderPath);//this is where JS finally runs OK
var homefolder = folder.getFolder("MalaChat"); //THIS IS ERROR No 1 'folder.getFolder is not a function'
var file = homefolder.getFile("my.db");  //THIS IS ERROR No 2 'homefolder.getFile is not a function'
console.log(file);

but according to the Docs, on file system there is a function getFolder and getFile!!!
where am i going wrong or how is it Done? @tjvantoll @jen.looper


#2

@devyaz as you can see from the docs fs.path.join(...) will return a string with a path. So it’s normal for a string to not have a getFolder or getFile function. You can read on how to operate with the file system in the docs.


#3

but that is in the app folder, i want it in my external storage like SD card but my code creates files and folders in the app directory e.g /data/data/AppName/,
var folderPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOCUMENTS).toString(); console.log(folderPath);//shows SD card path;
and thats androids way but {N} own way (knownfolders) is in the App directory!! am trying to “hack” because there’s no documentation on External storage!!


#4

this is what you want
https://docs.nativescript.org/cookbook/file-system


#5

as i said before i can create folder and a file BUT it creates in the Devices protected Directory, iwant it in the External storage like SD Card!! Please

var path = fs.path.join(fs.knownFolders.documents().path, "music");
var folder = fs.Folder.fromPath(path);

this from the docs and it does work but NOT in SD Card!!
this can get to SD card


[quote="devyaz, post:3, topic:3019"]
var folderPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOCUMENTS).toString(); console.log(folderPath);//shows SD card path;
[/quote]


but i dont Know How to Create a Folder Or Create a File in SD Card!! @triniwiz @plamen5kov


#6

What android api are you using ?


#7

api 22 a.k.a Lolipop 5.1


#8

@triniwiz @tjvantoll his is how i managed to write to SD Card
var path = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOCUMENTS).toString(); var path1 = fsModule.path.join(path, "FolderName"); var folder = fsModule.Folder.fromPath(path1).toString(); //this is where {N} writes to Androids Native path above
any ideas for IOS??


File-system sd-card
#9

Refer below mentioned links may helps someone to get good start about key point of this topic discussion.

  • Link 1
  • Link 2
  • Link 3

https://developer.android.com/guide/topics/data/data-storage.html

  • Link 4

Awesome stackoverflow post -

Good luck!


#10

No longer working after upgrade to tns-cli 3.4.3 or 3.4.2 .even using the permission plugin which I was using (it just denies permissions now) from kitkat to O


#11

UPDATE use android.os.Environment.getExternalStorageDirectory().getAbsolutePath().toString()
instead for android.os.Environment.getExternalStorageDirectory().getAbsolutePath() bcoz after updating it quit working


#12

hi @devyaz, thanks for your solution.
any idea to access sd card?
your solution would enable to access device internal storage. but not sd card.
For example,
sd card path is /storage/sdcard1
When I call var folder: fs.Folder = fs.Folder.fromPath(" /storage/sdcard1"), it gave not accessible error: EACCESS. while external permissions have been set up.


#13

Hi @ashley , how did you get that location? is it spposed to be /storage/emulated/0/sdcard
var path = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOCUMENTS).toString(); var path1 = fs.path.join(path, "FolderName"); var folder = fs.Folder.fromPath(path1).toString(); //this is where {N} writes to Androids Native path above
This will get to External storage or Sd card in the Documents folder


#14

I got this in Android studio terminal. For some of android devices, their sd card path would be like this.
Can I just write files to sd card when I already know about this path?
Otherwise, I need to write a plugin to open sd card for this app.


#15

hi @ashley, this =>
var directory=android.os.Environment.getExternalStorageDirectory().getAbsolutePath().toString()

will give “/storage/emulated/0/”

then do
var path1 = fs.path.join(directory, "FolderNameYouWantToCreate"); var folder = fs.Folder.fromPath(path1).toString(); or a hard coded way wil be =>

 var path = fs.path.join('/storage/emulated/0/', "FolderNameYouWantToCreate");
 var folder = fs.Folder.fromPath(path).toString();

now check in your internal emulated Sd storage(hence the /storage/emulated/0/ which if your device doesn’t have an Sd card slot, will use the public internal partition as Sdcard!) or SD card
i hope it clears the mist now! :slight_smile:


Get external storage path(sd card)
#16

try this snack Create folder in Sd/internal Sd on {N}Palyground First Run creates folder in Internal Sdcard, then tap the button to(hard coded path, check home-page.js) create in actual removable Sdcard!!(in Samsung works) enjoy Nativescript ROCKS!!!
Edit
in short removable sdcard path => /storage/extSdCard/


#17

Hi @devyaz, thanks a lot for your help.
It seems like it is my device setting issue. I still cannot access this path.
Now, I am thinking build a small plugin for this since I can access it by running bash command.
Still appreciate your help.