Jack He
Jack He

Reputation: 1693

Not able to upload image from gallery in Android 4.4

I am trying uploading images which are gotten from gallery with the code below.

//imageUri is a uri which is gotten by Phonegap camera API 
//(use option Camera.PictureSourceType.PHOTOLIBRARY&&Camera.DestinationType.FILE_URI) 
var imageUri = getImageUri();

//handlerUri is a uri to upload the image 
var handlerUri = getHandlerUri();

window.resolveLocalFileSystemURI(imageUri, function(fileEntry) {
    fileEntry.file(function(fileObj) {
        var fileName = fileEntry.name;
        var options = new FileUploadOptions();
        options.headers = {
            Connection: "close"
        };
        options.fileKey = "uploadfiles";
        if (fileName.indexOf('/') > -1) {
            options.fileName = fileName.substr(fileName.lastIndexOf('/') + 1);
        } else {
            options.fileName = fileName;
        }

        options.mimeType = "image/jpeg";
        options.chunkedMode = false;
        options.params = params;

        var ft = new FileTransfer();
        imageUri = fileEntry.nativeURL;

        ft.upload(imageUri, handlerUri,
            function(r) {
                if (r.response == "success") {

                } else {

                }
            },
            function(error) {},
            options, true);
    }, function(error) {

    });
}, function(error) {

});

For devices below Android 4.4, the app works well. However, in Android 4.4 KitKat, the same code does not work anymore.

Note: I have only gotten a code 3 error in LogCat after long time uploading. Someone says that code 3 is a connection error but it should not be my condition because the same code works well below Android 4.4.

Could anyone help?

Thanks.

Upvotes: 0

Views: 546

Answers (1)

Jack He
Jack He

Reputation: 1693

I have finally worked it out by myself. It seems that the wrong file name is the root. When I get the file name with the old code

var fileName = fileEntry.name;

the file name is 7962 which is not correct.

I use the code below to resolve this issue:

var fileName = fileEntry.nativeURL.substr(fileEntry.nativeURL.lastIndexOf('/') + 1);

Thanks for all guys.

Upvotes: 1

Related Questions