File upload using cordova-plugin-file-transfer not working on Android devices - works only on iOS devices

Hello. Does anyone have any idea what might be the problem? For the last weeks i’ve been trying to figure out why my code works only on iOS and not on Android devices. Any idea would be higtly appreciated since i run out of options.

On the android device it shows to send the file but on the server i don’t recive any informations (nor the picture neither the rest of the parameters). On the other hand on iOS all works just fine, and the server also.

My code:
home.html:

<button ion-button color="dark" (click)="takePhoto()">Open camera </button>
<img [src]="imageURL" *ngIf="imageURL" />
<br>imageURL={{imageURL}}
<br><button ion-button color="dark" (click)="upload()">Upload</button>

home.ts:
> import { Component } from ‘@angular/core’;
> import { NavController } from ‘ionic-angular’;
> import { Camera } from ‘ionic-native’;
> import { Transfer } from ‘ionic-native’;

@Component({
      selector: 'page-home',
      templateUrl: 'home.html'
})
export class HomePage {
    imageURL: any;
    myInput: string;
    cameraUrl: string;

    constructor(public navCtrl: NavController) {
    }

   takePhoto(){
       Camera.getPicture().then((imageData) => {
            this.imageURL = imageData;
       }, (err) => {
            console.log(err);
       });
   }
   upload(){
       var ft = new Transfer();
       var options = {
       fileKey: 'file',
       fileName: 'filename.jpg',
       params:{operatiune:'uploadpoza'}
   }
   ft.upload(this.imageURL,encodeURI("https://www.myserver.com/test.php"),options)
       .then((data) => {
            // success
            alert("Image send:"+this.imageURL);
        }, (err) => {
          // error
          alert("err: "+JSON.stringify(err));
        })
    }
}

I’ve checked the errors in two ways:

Case 1:

I have 2 alerts in my uploaded code:
a. a message in case of success: alert(“image send:”+this.imageURL);
b. a message for error: alert("err: "+JSON.stringify(err));

In my case, I noticed that only the first message appears, the one where
the code is executed correctly.

Case 2:

I’ve connected my phone through cable to the laptop.
To launch the app I typed in “Node.js-Command-Prompt” the command “ionic run
Android”.
I’ve opened Chrome browser and typed in the address bar
“chrome://inspect/#devices”. Then select “INSPECT-option” for my app and
popup the console. The console didn’t show any error.
Is this the right way to test it, or should I try something different?

I printed the object “data” received in case of success for “ft.upload”
function. The result are:

data.response=
data.responseCode=200
data.bytesSent=411240

For me this means that information is sent to the server.

One more thing:

  • I’ve run different kinds of tests: sending information to both servers
    with HTTPS and HTTP
  • I’ve tried adding the trustAllHosts=true and in another test
    trustAllHosts=false

And every time the same problem: works on iOS but fails on android

Details about the solution:http://stackoverflow.com/questions/41879787/file-upload-using-cordova-plugin-file-transfer-not-working-on-android-only-on-i

Hello could you please explain how you get the image in the server side because I have the same problem and even with chunkedMode: false I still can’t get the image in the server !!

Hello,
This exemple, with chunkedMode: false works for me

Let me know if it works for you too

As far as I know, it wont work on android because of permission issues. Hence you need to copy the image to a folder before making reference to it. You can use platform to check if you are on android or ios.

I have a blog post on this here http://www.developerslearnit.com/2016/11/build-photo-gallery-app-with-ionic2.html

Hope that helps

I just updated the above link.

Great Solution you save me , Barakallah