The change was at PhoneGap’s end. For some reason, PhoneGap Build’s default version of the File library isn’t in PhoneGap Build. It’s a bug in PhoneGap.
The workaround is easy. In Project Properties, open PhoneGap configxml. Change the line for cordova-file-plugin so that it has the version number for the library:
It took me two days and night (I hate the poor ability to debug PhoneGap builds…) to finish my task:
read/write text files and read mp3 files to/from public main (root) directory of my android device.
But I made it… finaly… here is the code:
You need 4 Buttons: btWrite, btReadTxt, btReadMp3, btPath as well as
audio1 control and TextArea1.
Permission for Cordova File Plugin needs to be set in config.xml:
< gap:plugin name=“cordova-plugin-file” source=“npm” version=“3.0.0”/>
Dim folder
Dim fileName
Dim dataToWrite = "This is the data to write." 'what needs to be written in txt file
'Where to Store Files (Android):
'cordova.file.dataDirectory - Persistent and private data storage within the application's sandbox
'cordova.file.cacheDirectory - The OS may delete these files when the device runs low on storage
'cordova.file.externalApplicationStorageDirectory - Application Space On external storage
'cordova.file.externalDataDirectory - App-specific data files On external storage
'cordova.file.externalCacheDirectory - Application cache On external storage
'cordova.file.externalRootDirectory - External storage (SD card) root
Function btPath_onclick()
MsgBox(cordova.file.externalRootDirectory)
End Function
'############ READ txt / mp3 file
Function btReadTxt_onclick()
folder = "storage"
fileName = folder & "/" & "text.txt"
Call ReadFilePart1of2()
End Function
Function btReadMp3_onclick()
folder = "storage"
fileName = folder & "/" & "ding.mp3"
Call ReadFilePart1of2()
End Function
Sub ReadFilePart1of2()
'needs public var: folder, fileName
'window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, gotFileSystemR, failPermissionR)
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, gotFileSystemR, failPermissionR)
End Sub
Function failPermissionR(error)
Call AddLog("no permission for folder")
End Function
Sub gotFileSystemR(fs)
dataDir = fs.getDirectory(folder, {create: True})
fs.getFile(fileName, {create: False }, gotFileEntryR, failExistR)
End Sub
'if doesn't exist
Sub failExistR(error)
Call AddLog("This file does not exist!")
End Sub
'if exist...
Sub gotFileEntryR(fe)
fe.file(gotFileReader, failGetFileReader)
End Function
Function failGetFileReader(error)
Call AddLog("Could not read file")
End Function
Function gotFileReader(fileBlob)
reader = new FileReader()
'if it's a mp3
reader.onload = readSuccess
reader.readAsDataURL(fileBlob)
'if it's a text file
'reader.onloadend = GetRef(readData)
'reader.readAsText(fileBlob)
'Alternative from https://github.com/cfjedimaster/Cordova-Examples/blob/master/readtextfile/www/js/app.js
'var reader = new FileReader();
'reader.onloadend = Function(e) {
' console.log("Text is: "+this.result);
' document.querySelector("#textArea").innerHTML = this.result;
'}
'reader.readAsText(file);
End Function
Function readSuccess(e)
AddLog("file content: " & this.result)
'if it's a mp3
Audio1.src = this.result
Audio1.play()
End Function
'######## WRITE txt file
Function btWrite_onclick()
folder = "storage"
fileName = folder & "/" & "text.txt"
Call WriteFilePart1of2()
'to be continued at WriteFilePart2of2...
End Sub
Function WriteFilePart1of2()
'needs public var: folder, fileName, dataToWrite
'window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, gotFileSystemW, failPermissionW)
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, gotFileSystemW, failPermissionW)
End Function
Function failPermissionW(error)
Call AddLog("no permission for folder")
Call WriteFilePart2of2()
End Function
Function gotFileSystemW(fs)
dataDir = fs.getDirectory(folder, {create: True})
fs.getFile(fileName, {create: True }, gotFileEntryW, failExistW)
End Function
Function failExistW(error)
Call AddLog("file does not yet exist")
End Function
Function gotFileEntryW(fe)
fe.createWriter(gotFileWriter, failGetFileWriter)
End Function
Function failGetFileWriter(error)
'AddLog("Error writing: " + error.code)
Call WriteFilePart2of2()
End Function
Function gotFileWriter(fw)
'fileWriter = fw
fw.onwrite = GetRef(writeSuccess)
fw.write(dataToWrite)
'Alternative from https://phonegap.com/blog/2014/11/05/cordova-example-writing-to-a-file/
'var blob = new Blob([Log], {type:'text/plain'});
'fw.write(blob);
End Function
Function writeSuccess(evt)
Call WriteFilePart2of2(True)
End Function
Sub WriteFilePart2of2(wasOkay)
If wasOkay = True Then
Call AddLog("write: ok")
Else
Call AddLog("could not write")
End If
End Sub
'######### log
Sub AddLog(txt)
TextArea1.text = txt & vbCRLF & TextArea1.text
End Sub