File-system sd-card



Any advice that how to store a file like “Test.txt” in sd-card with angular?


Angular doesn’t matter here, its all about Android. Use External Storage Directory while writing the file.

Here is an example how you will get the path,

Note: Make sure you have necessary permissions to write


Hi again, I have a problem when I run my code in console, it shows
“/storage/emulated/0/Download/test.txt”. But this isn’t my path of my smartphone.

This is my code:


<Button text=“ReadFile” (tap)=“onTap()”>


import { Component } from “@angular/core”;
import * as fs from “file-system”
declare var android: any; <— In angular I need this because I had problems with “android.os.Enviroment…”

selector: “my-app”,
templateUrl: “./app.html”
export class AppComponent {


var downloads = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString();
var text = fs.path.join(downloads,“test.txt”);


If you don’t undestand my english let me know, I will try to get better my english.



Sorry, my bad. External storage need not to be always SD card. Try getExternalFilesDirs


First thankyou for ask me, Hi again I checked getExternalFiles when I run the app does this:

JS: ERROR TypeError: Cannot read property ‘getExternalFilesDir’ of undefined
JS: ERROR CONTEXT [object Object]
JS: Angular is running in the development mode. Call enableProdMode() to enable the production mode.
13:13:18 - Compilation complete. Watching for file changes.

But when I refresh the app does this:

Successfully transferred app.component.ts.
Successfully transferred app.component.js.
Refreshing application…
JS: /storage/emulated/0/Android/data/org.nativescript.filepath/files/DCIM/test.txt
JS: Angular is running in the development mode. Call enableProdMode() to enable the production mode.
Successfully synced application org.nativescript.filepath on device 4d6bce1d.

I mean that gerExternalFilesDir works well when I refresh the app,
I took the file-system-path sample, I don’t know what’s going on with my code. Well my android version is Marshmallow 6.0 and my smartphone is a Lenovo K5. I suppose that the problem is the android. This is my code of app.component.ts:

import { Component, OnInit } from “@angular/core”;
import * as fs from “file-system”;
declare var android: any;
var appModule = require(“application”);

selector: “my-app”,
templateUrl: “./app.html”
export class AppComponent implements OnInit {

public documents: string;
public currentApp: string;
public temp: string;
public filePath: any;
public tempFilePath2: any;

public textContentToBeSaved: string = “”;
public isContentSaved: boolean = false;
public savedContent: string;


let documentsFolder = fs.knownFolders.documents();
let currentAppFolder = fs.knownFolders.currentApp();
let tempFolder = fs.knownFolders.temp();

let testPath = "///test.txt";

this.documents = fs.path.normalize(documentsFolder.path + testPath);
this.currentApp = fs.path.normalize(currentAppFolder.path + testPath);
this.temp = fs.path.normalize(tempFolder.path + testPath);

//documentsFolder = fs.knownFolders.documents();

//It works well when I run the app
//var downloads = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString();
//var downloads = android.os.Environment.getExternalStorageDirectory().toString();
//this.filePath = fs.path.join(downloads,"miCarpeta", "test.txt");

//It works well when I refresh the app but when I run the app it doesn't work
this.filePath =;
//var filePath =;
this.tempFilePath2 = fs.path.join(this.filePath,"test.txt");

And this my html code:

And the main problem is in the path yet, I cannot create the new file in my external storage of my smartphone,so the new file is store in a protected folder. Can you check my code please. I work in nativescript angular


First thing, currentContext is deprecated one try context / foregroundActivity. Second, it’s not guaranteed that the context scope is ready during init of your app component. May be try it in AfterViewInit or the root component’s loaded event.


Thanks for reply, I had used your advices, they work well, when I run the code do this:

output: /storage/emulated/0/Android/data/org.nativescript.filesample/files/Folder/test.txt

It works well, but I want this for example:

/internal storage/DCIM/Folder/test.txt



Well, I check this link:

but it doesn’t work like I want and I see this in android documentation in Save a file on external storage

Caution: The external storage might become unavailable if the user removes the SD card or connects the device to a computer. And the files are still visible to the user and other apps that have the READ_EXTERNAL_STORAGE permission. So if your app’s functionality depends on these files or you need to completely restrict access, you should instead write your files to the internal storage.

I checked my code when I disconnected my smartphone and it doesn’t work like I want

Can you check please, this is my code:


import { Component } from “@angular/core”;
import * as fs from “file-system”;
declare var android;
var appModule = require(“application”);

selector: “my-app”,
templateUrl: “./app.html”
export class AppComponent {

public filePath: any;
public tempFilePath2 : any;

this.filePath =;
this.tempFilePath2 = fs.path.join(this.filePath,“Folder”,“test.txt”);

//var downloads = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM).toString();
//var downloads = android.os.Environment.getExternalStorageDirectory().toString();
//this.tempFilePath2 = fs.path.join(downloads,"miCarpeta", "test.txt");




And I’m sorry to ask you again but I’m new in nativescript :sweat_smile: and the project that I work it’s a little hard.


I had same problem here.
Like let sdCardPath = fs.path.join(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM).toString());
Really just gave you back the internal storage. Not sd card path.
If you are just looking for the internal one, above code should be helpful.
As for sd card, there is no general method to get its path since it varies among devices.
What I suggest, run

let sdPath: fs.Folder =fs.path.join(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM).toString());
            sdPath.getEntities().then(function (entities) {
                entities.forEach(function (entity) {
                    if ( == "DCIM") {
                        var folder: fs.Folder = fs.Folder.fromPath(entity.path);
                        console.log("Entity Path " + folder.path)
                        try {
                            var testFile: fs.File = folder.getFile("test.txt");
                        } catch (e) {
                            console.log("Save error " + e);
            }, function (error) {
            console.log("Path " + sdPath.path);
            } catch (e) {

To check all subfolders in your external storage and see if you could write in.