簡體   English   中英

Crosswalk Cordova Android多文件選擇

[英]Crosswalk Cordova Android multiple file select

我有一個使用cordova和angularjs構建的混合應用程序,對於Android我使用人行橫道運行應用程序。

我一直在網上搜索html5文件輸入的解決方案,以允許選擇多個文件。

我正在使用以下元素進行文件選擇:

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

我正在運行Android Lollipop版本5.1.1和Crosswalk版本20,我已經使用Crosswalk版本18和19進行了測試。 Chrome已安裝在運行最新版本的設備上,但我認為這不會產生任何影響。

當我點擊上面的輸入元素時,我得到了預期的對話框,要求我從我的文檔或相機中進行選擇。 如果我選擇從我的文檔中選擇,那么我只能選擇單個文件,在這種情況下是圖像。 對於我可以從中選擇圖像的每個應用程序都是如此,因此默認的Android“圖像”,“視頻”,“音頻”等以及Google照片等外部應用程序 - 所有這些只允許我一次選擇一個文件。

在下面的圖像中,您可以看到列出的文件,長按每個圖塊不會將文件添加到多個選擇中。

在此輸入圖像描述

這適用於App的IOS版本。

在深入了解我可以在網上找到的所有材料后,似乎Android 5+運行Chrome 49+支持多重屬性。

我不確定這是一個人行橫道瀏覽器實現還是Android操作系統問題,還是其他什么? 任何人都可以建議。

編輯

只是為了確認這與使用或不使用Crosswalk無效。

經過數周的努力解決這個問題,我終於開始工作了(沒有Crosswalk的Cordova)。 這是在Windows中使用Cordova Tools完成的,所以請原諒下面的文件規范。

步驟1:將platforms \\ Android \\ CordovaLib \\ AndroidManifest.xml中的minSdkVersion更改為21 說明: onShowFileChooser API在LOLLIPOP(API 21)中引入。 它允許返回url[]而不是早期API版本中showFileChooser返回的url 僅在將API更改為21或更高時才會調用此方法。

第2步:更新/替換onActivityResult方法以檢索多個文件。 使用fileChooserParams創建intent后添加以下fileChooserParams以允許選擇多個文件:

    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

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

步驟3:使用intent.getClipData()更新相應的onActivityResult方法以返回多個URL。

注意事項:

  1. 為所有呼叫啟用多重上傳。 您可以根據fileChooserParams模式更新intent。
  2. 禁用相機作為選擇器中的源,默認情況下可用於人行橫道。

最終守則:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM