Ionic-native: Issues with writeFile on Android

Created on 5 Dec 2017  路  1Comment  路  Source: ionic-team/ionic-native

I'm submitting a ... (check one with "x")
[x] bug report

Current behaviour:
I am having issues with File.writeFile method. It works on iOS but doesn't work on Android.
I am saving a blob using the code below

this.file.resolveLocalFilesystemUrl(this.file.dataDirectory).then( (dir) => {
 console.log("Access to the directory granted succesfully");
            console.log(dir);
            console.log(this.file.dataDirectory);

            let name = filename + "template.html";
            console.log(name);
            this.file.writeFile(this.file.dataDirectory, name, DataBlob, {replace:true}).then(
              res => {
                console.log(res);
                return resolve(dir.fullPath);
              },
              error => {
                console.log(error);  
                return reject(error);
              }
            ).catch(error => {
              console.log(error);  
              return reject(error);
            });
          },
          error => {
            this.log('JTMTemplateDownload: savebase64, resolveLocalFilesystemUrl', error);
            return reject(error);
          });

The above code works fine on iOS but on Android it never gets into promise function for first time. When I run app second time. It gives me PATH_EXISTS_ERR. I checked directory using file manager but no file exist on that specific path

Expected behavior:
It should be writing file on given directory

Ionic info: (run ionic info from a terminal/cmd prompt and paste output below):
cli packages: (/Users/abdulwahab/Documents/mobileapps/JTMNew/itglobal2/node_modules)

@ionic/cli-utils  : 1.17.0
ionic (Ionic CLI) : 3.17.0

global packages:

cordova (Cordova CLI) : 7.1.0

local packages:

@ionic/app-scripts : 2.1.4
Cordova Platforms  : android 6.2.3 ios 4.4.0
Ionic Framework    : ionic-angular 3.6.1

System:

Android SDK Tools : 25.2.5
ios-deploy        : 1.9.2 
Node              : v8.4.0
npm               : 5.3.0 
OS                : macOS Sierra
Xcode             : Xcode 8.3.3 Build version 8E3004b

Environment Variables:

ANDROID_HOME : /usr/local/Cellar/android-sdk/24.4.1_1

Misc:

backend : legacy

Most helpful comment

We had the same issue but were able to solve it.
Instread of a Blob we are passing now a ArrayBuffer as an argument in the writeFile function.

In the process of changing from Blob to ArrayBuffer we found it, that the global FileReader() was replaced by an Implementation from the Cordova-File plugin. https://github.com/apache/cordova-plugin-file/blob/master/www/FileReader.js
We hat to secure the global FileReader on application start into our own variable to use it later.
Maybe this Problem has something to to with the Ionic Plugin to fail writing a Blob?

edit: have a look at this issue https://github.com/ionic-team/ionic-native/issues/505
edit2: changing the polyfill order (index.html) as mentioned in the issue fixed all our problems

>All comments

We had the same issue but were able to solve it.
Instread of a Blob we are passing now a ArrayBuffer as an argument in the writeFile function.

In the process of changing from Blob to ArrayBuffer we found it, that the global FileReader() was replaced by an Implementation from the Cordova-File plugin. https://github.com/apache/cordova-plugin-file/blob/master/www/FileReader.js
We hat to secure the global FileReader on application start into our own variable to use it later.
Maybe this Problem has something to to with the Ionic Plugin to fail writing a Blob?

edit: have a look at this issue https://github.com/ionic-team/ionic-native/issues/505
edit2: changing the polyfill order (index.html) as mentioned in the issue fixed all our problems

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FdezRomero picture FdezRomero  路  4Comments

danbucholtz picture danbucholtz  路  3Comments

GunaSekhar1 picture GunaSekhar1  路  4Comments

shaliko picture shaliko  路  3Comments

kyleap picture kyleap  路  4Comments