I want to export file to /sdcard/Documents/ or /storage/emulated/0/Documents/
I have needed permission on application declaration
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
I try to use File class from Nativescript
const extFolder: Folder = knownFolders.externalDocuments()
const extOutPath = path.join(extFolder.path, '123.json');
const extOutfile = File.fromPath(extOutPath);
but last line (256 on screen) always produced error
ERROR Error: java.io.IOException: Operation not permitted
I try to write my own function, based on native android Java function, something like that (I don’t sure that is correct function, I’m not Java Guru), but in Nativescript we have access to android.os.Environment and java.io.FileInputStream
exportFileToExternalStorage(fullInputFilePath, targetPath, fileName) {
request(targetPath + fileName).then((response) => {
console.log(JSON.stringify(android.os.Environment.DIRECTORY_DOCUMENTS))
try {
var myInput = new java.io.FileInputStream(
fullInputFilePath
);
var myOutput = new java.io.FileOutputStream(targetPath + fileName);
var buffer = java.lang.reflect.Array.newInstance(
java.lang.Byte.class.getField("TYPE").get(null),
1024
);
var length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
} catch (err) {
console.info("Error", err);
alert(err.message)
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
})
}
but faced with the same error in attempt to write to /sdcard/Documents/ or /storage/emulated/0/Documents/
What going wrong? How to overcome this restriction?