Cordoba fileTransfer works fine on iOS, gives error code = 1 on Android

I am developing a mobile application for iOS and Android using the Cordova and Ionic Framework. There should be a “Send Photo” and related functionality, and I use Cordova FileTransfer for this.

It works great on an iOS simulator, but it throws an "error code = 1" on an Android device.

I know what that means file_not_foundor the like.

Notice that this will happen if I take a picture from the camera or select one from the gallery.

Here is my code:

$scope.takePic = function() {
        var options =   {
            quality: 50,
            destinationType: navigator.camera.DestinationType.FILE_URI,
            sourceType: 0,      // 0:Photo Library, 1=Camera, 2=Saved Photo Album
            encodingType: 0     // 0=JPG 1=PNG
        }
        navigator.camera.getPicture(onSuccess, onFail, options);
    }
    var onSuccess = function(FILE_URI) {
        window.resolveLocalFileSystemURL(FILE_URI, function(fileEntry) {
            alert("full: " + JSON.stringify(fileEntry));
            var realUrl = fileEntry.toURL();
            $scope.picData = realUrl;
            $scope.$apply();
            console.log("real URL", realUrl);
        });
    };
    var onFail = function(e) {
        console.log("On fail " + e);
    }
    function win(r) {
        console.log("Code = " + r.responseCode);
        console.log("Response = " + r.response);
        console.log("Sent = " + r.bytesSent);
        Flash.success("Wysłano");
        var response = JSON.parse(r.response);
        $scope.attachment_id = response.data;
        $scope.$apply();
        $http.post($rootScope.baseServerUrl + 'Members/changeAvatar', {attachment_id: response.data}).success( function (response){
            console.log(response);
        });
    }

    function fail(error) {
        alert("An error has occurred: Code = " + error.code);
        console.log("upload error source " + error.source);
        console.log("upload error target " + error.target);
    }
    $scope.send = function() {
        Flash.warning('wysyłam');
        var myImg = $scope.picData;
        alert(myImg);
        var options = new FileUploadOptions();

        options.headers = {
            Accept: "application/json",
            Connection: "close"
        }
        options.fileKey="file";
        options.fileName=$scope.picData.substr($scope.picData.lastIndexOf('/')+1);
        options.mimeType="image/jpeg";
        options.chunkedMode = false;
        var ft = new FileTransfer();
        ft.upload(myImg, encodeURI($rootScope.baseServerUrl + 'media/Attachments/add'), win, fail, options);
    }

$scope.takePicand sendcalled by pressing the buttons. There are many warnings and consoles because I am trying to find why it is not working.

After selecting the image from the gallery on the android, I get:

:///storage/sdcard0/download/file-name.jpg

iOS:

:///Users//Library/Application%20Support/iPhone%20Simulator/7.1/Applications/B5FB2081-54E7-4335-8856-84C6499E6B07/tmp/cdv_photo_038.jpg

, <img src="{{picData}}">, .

Android-, Code = 1. iOS sim , , , ... .

, FileTransfer .

+4
2

, , - .

:

<input id="file" name="file" type="file" onchange="angular.element(this).scope().addFile(this)" class="upload" accept="image/*" capture="camera"/>

$scope.addFile() , :

$scope.addFile = function(item){

        function uploadComplete(evt) {
            /* This event is raised when the server send back a response */
            $scope.imgId = JSON.parse(evt.target.responseText).data;
            $scope.$apply();
            $http.post($rootScope.baseServerUrl + 'Members/changeAvatar', {attachment_id: $scope.imgId}).success( function (response){
                console.log(response);
                $scope.User.attachment_id = $scope.imgId;
                $scope.$apply();
            });
        }
        function uploadFailed(evt) {
            alert("There was an error attempting to upload the file.")
        };
        var updateImage = function (element) {
            $scope.$apply(function() {
                $scope.theFile = element.files[0];

                var formData = new FormData();
                formData.append("file", $scope.theFile);

                var xhr = new XMLHttpRequest()

                xhr.addEventListener("load", uploadComplete, false)
                xhr.addEventListener("error", uploadFailed, false)
                xhr.open("POST", $scope.baseServerUrl + "media/Attachments/add")
                xhr.setRequestHeader("Accept","application/json")
                $scope.progressVisible = true

                xhr.send(formData);
            });
        };
        updateImage(item)
    }

Android-, , 4.0, 4.4 - input type="file" bug, iOS- 8.1 ( , ).

, , . , Cordova FileTransfer : ", " , , , -...

, , - . , .

0

, , file:///storage.sdcard0/download/file-name.jpg file:///storage/sdcard0/download/file-name.jpg, .

- , , . , , URI (, , )? -, 1.0, / .

-cli, @ cordova plugin add org.apache.cordova.file@1.0.0

/ github #, cordova plugin add https://github.com/apache/cordova-plugin-file-transfer#r0.4.2

0

All Articles