Syed
Syed

Reputation: 1452

Android kitkat: fileChooser + fileTransfer cordova plugin not working

I am building an android application using apache cordova framework.

In android 4.4 won't work as described in following issue

https://issues.apache.org/jira/browse/CB-5294

For that I am using below filechooser plugin

https://github.com/don/cordova-filechooser

Till now I am able to open file chooser dialog and select the file.

When I try to upload the file using following code

                var def = Q.defer();
                var options = new FileUploadOptions();
                options.fileKey = "file";
                options.fileName = file.substr(file.lastIndexOf('/') + 1);
                var params = {};
                params.title = title;
                options.params = params;
                options.trustAllHosts = true;
                var ft = new FileTransfer();
                var url = Constants.API_SERVICE_URL + "upload";
                ft.upload(file, encodeURI(url), function(res) {
                    // check for HTTP OK status and success
                    if(res.responseCode === 200 && res.response && res.response.status !== "fail") {
                        def.resolve(res.response);
                    } else {
                        def.reject(res);
                    }
                    def.resolve(res);
                }, function(err) {
                    def.reject(err);
                }, options);
                return def.promise;

The file here would be something like

content://com.android.providers.media.documents/document/image:28029

I got java.lang.SecurityException

I did so much research on the topic, but I am not able to find answers.

This issue is described here https://issues.apache.org/jira/browse/CB-5398

The issue there is marked as resolved, but the workaround described in this comment is not working

https://issues.apache.org/jira/browse/CB-5398?focusedCommentId=13977552&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13977552

If I do that I am getting file not found exception, the file url is like

file://storage/emulated/0/images/image.png

The below stackoverflow question is related

Kitkat, error while trying to load an image

The question has an answer, I tried that also but that is also not working.

I added MANAGE_DOCUMENTS permission in my android manifest file List of all permissions

<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>  
<uses-permission android:name="android.permission.WAKE_LOCK"/>  
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>  
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>  
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

But if I choose the file from gallery option or old file manager. It is working fine.

Any idea of how I can resolve this ?

EDIT:

Apache cordova version 3.6

Upvotes: 1

Views: 3076

Answers (2)

jcesarmobile
jcesarmobile

Reputation: 53351

Try with the Cesidio DiBenedetto plugin instead of the one you used https://github.com/cdibened/filechooser

Here you have a sample project using it

https://github.com/jcesarmobile/FileBrowserAndroidTest

Upvotes: 1

Jack He
Jack He

Reputation: 1693

You asked this question at the right time! I was struggling for days and I have worked out it.

The code below works well for Android version below 4.4 but not for kitkat.

options.fileName = file.substr(file.lastIndexOf('/') + 1);

I have created an Android project and I found that when it comes to Android 4.4, the 'file'(file name) seems to be '7961'(or other number) instead of the actual name 'abc.png'. What we need is the code below.

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

Let me know if that is working for you.

Upvotes: 0

Related Questions