i'm having lot of troubles saving files in android.
project hybrid application developed ionic these plugins:
com.phonegap.plugins.fileopener 1.0.0 "file opener" com.telerik.plugins.nativepagetransitions 0.4.2 "native page transitions" cordova-plugin-compat 1.0.0 "compat" cordova-plugin-crosswalk-webview 2.0.0 "crosswalk webview engine" cordova-plugin-file 4.2.0 "file" cordova-plugin-network-information 1.2.2-dev "network information" cordova-plugin-whitelist 1.2.3-dev "whitelist" cordova-plugin-wkwebview-engine 1.0.4-dev "cordova wkwebview engine" ionic-plugin-keyboard 2.2.1 "keyboard"
android platform version 5.2.1
device i'm using samsung a7
this abstract androidmanifest.xml
<uses-sdk android:minsdkversion="16" android:targetsdkversion="23" /> <uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage" />
case1
if try snippet (actually working on project)
var storagepath = "/storage/emulated/0"; var filedir = cordova.file.externaldatadirectory.replace(cordova.file.externalrootdirectory, ''); var filename = $scope.ngdocument.documentid + ".pdf" var filepath = storagepath + "/" + filedir + filename; $cordovafile.writefile(filepath, binary_arr, {'append': false}).then(function(result) {}, function(err) {});
i {"code":5,"message":"encoding_err"}
callback $cordovafile.writefile
, no matter if use absolute path, relative path, file name , no file ever created.
case2
with snippet
window.requestfilesystem(localfilesystem.persistent, 0, function (fs) { console.log('file system open: ' + fs.name); fs.root.getfile(filename, { create: true, exclusive: false }, function (fileentry) { console.log("fileentry:" + json.stringify(fileentry)); writefile(fileentry, binary_arr); }, function(data){}); }, function(data){});
happen 2 different things
case 2.1
if no config options specified in config.xml
app creates empty folder /storage/emulated/0/android/media/{myapp}
case 2.2
with these 2 preferences
<preference name="androidpersistentfilelocation" value="compatibility" /> <preference name="androidextrafilesystems" value="cache" />
a file in /storage/emulated/0
(external ssd) created , in logcat
errors are:
e/vold ( 2280): failed find mounted volume /storage/extsdcard/android/data/{myapp}/files/ w/vold ( 2280): returning operationfailed - no handler errno 0 w/contextimpl(13364): failed ensure directory: /storage/extsdcard/android/data/{myapp}/files e/vold ( 2280): failed find mounted volume /storage/extsdcard/android/data/{myapp}/files/ w/vold ( 2280): returning operationfailed - no handler errno 0 w/contextimpl(13364): failed ensure directory: /storage/extsdcard/android/data/{myapp}/files e/vold ( 2280): failed find mounted volume /storage/extsdcard/android/data/{myapp}/cache/ w/vold ( 2280): returning operationfailed - no handler errno 0 w/contextimpl(13364): failed ensure directory: /storage/extsdcard/android/data/{myapp}/cache
the strange fact /storage/extsdcard
(symbolic link /mnt/extsdcard
) not mounted while external ssd mounted on /mnt/sdcard
please help: i'm headbanging.
first snippet working charm in project. version of ngcordova?
solved:
after several attempts solved in way
in config.xml
:
<preference name="androidpersistentfilelocation" value="compatibility" /> <preference name="androidextrafilesystems" value="files,cache, sdcard, cache-external, files-external" />
and main function:
window.requestfilesystem(localfilesystem.persistent, 0, function (fs) { //var abspath = "file:///storage/emulated/0/"; var abspath = cordova.file.externalrootdirectory; var filedir = cordova.file.externaldatadirectory.replace(cordova.file.externalrootdirectory, ''); var filename = "somename.txt"; var filepath = filedir + filename; fs.root.getfile(filepath, { create: true, exclusive: false }, function (fileentry) { writefile(fileentry, binary_arr).then(function(){ //do here }); }, function(err) {}); }, function(err) {}); function writefile(fileentry, dataobj) { return $q(function (resolve, reject) { fileentry.createwriter(function (filewriter) { filewriter.onwriteend = function () { resolve(); }; filewriter.onerror = function (e) { reject(e); }; filewriter.write(dataobj); }); }); }
it seemed that:
<preference name="androidpersistentfilelocation" value="internal" />
that default configuration, not allow app write external disk (whether physical or emulated). instead allowed app write /data/data/{myapp}/files
Comments
Post a Comment