Creating a new folder


#1

Hi guys, I want to save an image into a new directory, that I create through the code but I don’t succeed in doing that… The new directory is “AppEtice” inside the “Pictures” directory of Android. The system doesn’t even create it.

here the code

var fs = require("file-system");
          var path = fs.path.join(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM).getAbsolutePath(),"Camera","AppEtice",postDetails.dataCreation+"_"+result.key+".jpeg");          console.log(path);
          console.log(path);

              //This function get the source of the image chosen to be posted (the image is into "postDetails.myImage")
              imageSource.fromAsset(postDetails.myImage).then(function(source){
                
              if (source.saveToFile(path,"jpeg"))
                  console.log("Image stored successfully");
                  else
                  console.log("Error while storing the image");

#2

Did you create the folder?

https://docs.nativescript.org/cookbook/file-system

Get or Create a Folder With Path

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

#3

I have tried in that way but it doesn’t work… That function “fs.knownFolders.documents()” is relevant to the app folder, I want to create a folder into an external directory


#4

ok. What about permissions? Did you add something to the android manifest?

I haven’t used file stuff yet in NS. I just remember having to do that in my phonegap days.


#5

I got an error but I added

var folder = fs.Folder.fromPath(path)

in the example:

var fs = require("file-system");
          var path = fs.path.join(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM).getAbsolutePath(),"Camera");          console.log(path);
          console.log(path);
          var folder = fs.Folder.fromPath(path)

              //This function get the source of the image chosen to be posted (the image is into "postDetails.myImage")
              
}

not sure why yet.


#6

me too, if I add var folder = fs.Folder.fromPath(path) I get an error