2016-09-08 70 views
4

我有一個使用cordova和angularjs構建的混合應用程序,對於Android我使用人行橫道運行應用程序。Crosswalk Cordova Android多文件選擇

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

我使用下列元素文件選擇:

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

我運行Android棒棒糖版本5.1.1和人行橫道版本20,我與人行橫道版本18和19也測試。 Chrome安裝在運行最新版本的設備上,但我認爲這沒有什麼不同。

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

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

enter image description here

此作品在App的IOS版本。

經過挖掘我可以在網上找到的所有材料後,似乎在運行Chrome 49+的Android 5+上支持multiple屬性。

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

編輯

只是爲了確認這不使用或不使用人行橫道工作。

+0

同樣在這裏的問題。在人行橫道項目中製作了一個bugreport:https://crosswalk-project.org/jira/browse/XWALK-7352 –

+0

對此有何更新? –

回答

2

經過幾個星期的努力,我終於找到了工作(科爾多瓦沒有人行橫道)。這是使用Windows中的Cordova Tools完成的,所以請原諒下面的filespecs。

步驟1:在平臺\的Android \ CordovaLib \ AndroidManifest.xml中更改的minSdkVersion至21 說明:onShowFileChooser API是在棒糖引入(API 21)。它允許在早期的API版本中返回url[]而不是url返回的showFileChooser。只有當您將API更改爲21或更高時,纔會調用它。

步驟2:更新/替換onActivityResult方法來檢索多個文件。 追加以下使用fileChooserParams允許選擇多個文件創建後的意圖:

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 

地點:平臺\機器人\ CordovaLib的\ src \組織\阿帕奇\科爾多瓦\發動機\ SystemWebChromeClient。java

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

注意事項:

  1. 促成多上傳所有呼叫。您可以基於fileChooserParams模式更新意圖。
  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; 
} 
+0

這仍然是首選的方法? – Thomas

+0

理想情況下,這應該在科爾多瓦固定,但我不熟悉這樣做的機制。因此,如果有人可以自願在下一個版本中解決這個問題,包括測試,這將有助於每個人。 – RIBH