2016-04-28 83 views
1

我正在製作我自己的Android應用程序,90%的人需要文件上傳。文件上傳Android 4.4 WebView

我得到了WebView棒棒糖代碼,它工作正常,但是當我嘗試在Android 4.4中打開文件選擇器時,輸入無法正常工作。

任何人都可以幫助我嗎?這裏是我的代碼:

mWebView.setWebChromeClient(new WebChromeClient() { 

     public boolean onShowFileChooser(
       WebView webView, ValueCallback<Uri[]> filePathCallback, 
       WebChromeClient.FileChooserParams fileChooserParams) { 
      if(mFilePathCallback != null) { 
       mFilePathCallback.onReceiveValue(null); 
      } 
      mFilePathCallback = filePathCallback; 

      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) { 
       // Create the File where the photo should go 
       File photoFile = null; 
       try { 
        photoFile = createImageFile(); 
        takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath); 
       } catch (IOException ex) { 
        // Error occurred while creating the File 
        Log.e(TAG, "Unable to create Image File", ex); 
       } 

       // Continue only if the File was successfully created 
       if (photoFile != null) { 
        mCameraPhotoPath = "file:" + photoFile.getAbsolutePath(); 
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
          Uri.fromFile(photoFile)); 
       } else { 
        takePictureIntent = null; 
       } 
      } 

      Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); 
      contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); 
      contentSelectionIntent.setType("image/*"); 

      Intent[] intentArray; 
      if(takePictureIntent != null) { 
       intentArray = new Intent[]{takePictureIntent}; 
      } else { 
       intentArray = new Intent[0]; 
      } 

      Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); 
      chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); 
      chooserIntent.putExtra(Intent.EXTRA_TITLE, "Escoge tu imagen"); 
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); 

      startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE); 

      return true; 
     } 
    }); 

回答

-1

我試過你的代碼,並且看到了這個問題。檢查這些code example它適用於所有Android設備,它允許使用webviewclient和webchrome客戶端上傳Android應用程序中的圖像文件。

0

Android 4.4有一個打破文件輸入的WebView錯誤。我的解決方法是使用Javascript在應用程序和網站之間進行通信,並檢測用戶何時上傳文件並從應用程序(webview外部)上載,然後在文件加載完成時更新webview,以告訴該文件位於服務器中的網站。

請在這裏檢查我的答案,我在這裏解釋我的解決方案: https://stackoverflow.com/a/43443981/2566593