New directory create using file.createDir

Is it possible to create new directory in externalRootDirectory.

this.platform.ready().then(() =>{
	if(this.platform.is('android')) {
		this.file.checkDir(this.file.externalRootDirectory, 'Event').then(response => {
			console.log('Directory exists'+response);
		}).catch(err => {
			console.log('Directory doesn\'t exist'+JSON.stringify(err));
			this.file.createDir(this.file.externalRootDirectory, 'Event', false).then(response => {
				console.log('Directory create'+response);
			}).catch(err => {
				console.log('Directory no create'+JSON.stringify(err));
			}); 
		});
	}
});

No error shows.

Is it need extra permission to create new directory ?

What is this? Should we guess?

import { Platform  } from 'ionic-angular';
import { File } from '@ionic-native/file';
export class SessionDetail {
	constructor(private file: File,public  platform: Platform,){
	    this.platform = platform; 
		this.file = file; 
	}
	download(){
		this.platform.ready().then(() =>{
			if(this.platform.is('android')) {
				this.file.checkDir(this.file.externalRootDirectory, 'Event').then(response => {
					console.log('Directory exists'+response);
				}).catch(err => {
					console.log('Directory doesn\'t exist'+JSON.stringify(err));
					this.file.createDir(this.file.externalRootDirectory, 'Event', false).then(response => {
						console.log('Directory create'+response);
					}).catch(err => {
						console.log('Directory no create'+JSON.stringify(err));
					}); 
				});
			}
		});
	}
}

This is my code for creating directory.

2 Likes

Is it possible to create directory. I am testing in mobile Samsung galaxy j7 prime. And mobile android version 7.0.0.

Did you try the code form the example?

Sorry for late reply.
when i am trying this below code in “ionic cordova run android --device -l -c --address 192.168.0.103” mode then show in console “console.log: {“code”:1,“message”:“NOT_FOUND_ERR”}”

import { Platform  } from 'ionic-angular';
import { File } from '@ionic-native/file';
export class SessionDetail {
	constructor(private file: File,public  platform: Platform,){
	    this.platform = platform; 
		this.file = file; 
	}
	download(){
		if(this.platform.is('android')) {
			this.file.checkDir(this.file.dataDirectory, 'mydir').then(
				response => console.log(response)
			).catch(err => console.log(JSON.stringify(err)));
		}
	}
}

This error response " console.log: {“code”:1,“message”:“NOT_FOUND_ERR”}" is logged when it tries to create a file in a directory that doesn’t exist.

You need first to check if the directory exist, if not create it and then write the file. However, I have been strangling with this issue for a while and haven’t found a solution yet :confused:

You have to check first for permission if you are working over API level greater than 19

this.androidPermissions.hasPermission(this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE)
.then(status => {
if (status.hasPermission) {
this.downloadFile();
}
else {
this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE)
.then(status => {
if(status.hasPermission) {
this.downloadFile();
}
});
}
});