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??


#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!