Crosswalk Cordova Select multiple Android files

I have a hybrid application built using cordova and angularjs, for Android, I launch the application using a crosswalk.

I am browsing the internet to find a solution for entering an html5 file to allow multiple files to be selected.

I use the following element to select a file:

<input type="file" multiple="multiple" name="files[]" />

I am running Android Lollipop version 5.1.1 and Crosswalk version 20, I also tested versions of Crosswalk 18 and 19. Chrome is installed on my device with the latest version, although I do not think it matters.

When I click on the input element above, I get the expected dialog asking me to choose from my documents or camera. If I want to select from my Documents, I can only select individual files, in this case images. This is true for every application from which I can select images, so the default is "Images", "Video", "Audio", etc. Etc. And other applications, such as Google Photos, all this allows me to select only one file at a time,

In the image below you can see the listed files, a long press on each fragment does not add the file to the multiple selection.

enter image description here

This works in the iOS app version.

After going through all the materials that I can find on the Internet, it seems that the multiple attribute is supported on Android 5+ running Chrome 49+.

, Android - ? - .

Edit

, , Crosswalk.

+5
2

, , , , (Cordova without Crosswalk). Windows, , , .

1: minSdkVersion \Android\CordovaLib\AndroidManifest.xml 21 : onShowFileChooser API LOLLIPOP (API 21). url[] url, showFileChooser API. API 21 .

2: / onActivityResult . : fileChooserParams, :

    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

: \android\CordovaLib\src\org\apache\cordova\engine\SystemWebChromeClient.java

3: onActivityResult, URL-, intent.getClipData().

:

  • Multi-upload . fileChooserParams.
  • , .

:

Uri photoUri;

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onShowFileChooser(WebView webView, final ValueCallback<Uri[]> filePathsCallback, final WebChromeClient.FileChooserParams fileChooserParams) {
    // Check and use MIME Type.
    String mimeType = "*/*";
    int ACTION_CODE = FILECHOOSER_RESULTCODE;
    try {
        if (fileChooserParams.getAcceptTypes().length > 0) {
            mimeType = fileChooserParams.getAcceptTypes()[0];
        } else {
            mimeType = "*/*";
        }
    } catch (Exception e) {
        mimeType = "*/*";
    };

    // Check if Mutiple is specified 
    Boolean selectMultiple = false;
    if (fileChooserParams.getMode() == WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE) {
        selectMultiple = true;
    };

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    if (selectMultiple) { intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); };
    intent.setType(mimeType);
    ACTION_CODE = FILECHOOSER_RESULTCODE;
    final Intent chooserIntent = Intent.createChooser(intent, "Select Source");

    // Add camera intent to the chooser if image and send URI to return full image 
    if (mimeType.equals("image/*")) {
        photoUri = null;
        try {
            File photoFile = createImageFile();
            photoUri = Uri.fromFile(photoFile);
        }
        catch (Exception ex) {
            photoUri = null;
        }
        if (photoUri != null) {
            Intent camIntent = new Intent();
            camIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            camIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            camIntent.putExtra("return-data", true);
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent [] {camIntent} );
        }
    }

    try {
        parentEngine.cordova.startActivityForResult(new CordovaPlugin() {
            @Override
            public void onActivityResult(int requestCode, int resultCode, Intent intent) {
                if (resultCode ==  Activity.RESULT_OK && intent != null) {
                    if (intent.getData() != null)
                    {
                        Uri[] result = WebChromeClient.FileChooserParams.parseResult(resultCode, intent);
                        filePathsCallback.onReceiveValue(result);
                    }
                    else
                    {
                        if (intent.getClipData() != null) {
                            final int numSelectedFiles = intent.getClipData().getItemCount();
                            Uri[] result = new Uri[numSelectedFiles];
                            for (int i = 0; i < numSelectedFiles; i++) {
                                result[i] = intent.getClipData().getItemAt(i).getUri();
                            }
                            filePathsCallback.onReceiveValue(result);
                        }
                        else {
                            filePathsCallback.onReceiveValue(null);
                        }
                    }
                }
                else if(resultCode ==  Activity.RESULT_OK && (intent == null || intent.getData() == null )) {
                    Uri[] result = new Uri[1];
                    result[0] = photoUri;
                    filePathsCallback.onReceiveValue(result);
                } else {
                    filePathsCallback.onReceiveValue(null);
                }
            }
        }, chooserIntent, ACTION_CODE);
    } catch (ActivityNotFoundException e) {
        Log.w("No activity found to handle file chooser intent.", e);
        filePathsCallback.onReceiveValue(null);
    }
    return true;
}
+4

, . , .

0

Source: https://habr.com/ru/post/1654011/


All Articles