2017-02-22 23 views
1

我一直在努力嘗試從web視圖上傳文件。 我嘗試了很多解決方案,但都沒有工作。webview,文件輸入字段filechoos不顯示

http://m0s-programming.blogspot.tw/2011/02/file-upload-in-through-webview-on.html

Android WebView File Upload

文件選擇當我點擊按鈕,瀏覽器打開一個對話框,在這裏我可以選擇要上傳的文件在桌面瀏覽器Chrome等工作正常。

當我使用我的手機測試程序,我點擊選擇文件對話框沒有反應

有人能幫助我嗎?由於

回答

0

參見本文檔:

https://infeeds.com/d/CODEmgks/20475/upload-image-file-gallery-or-camera-webv

public boolean onShowFileChooser(
    WebView webView, ValueCallback<Uri[]> filePathCallback, 
    WebChromeClient.FileChooserParams fileChooserParams){ 
    if(mUMA != null){ 
      mUMA.onReceiveValue(null); 
    } 
    mUMA = filePathCallback; 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    if(takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null){ 
      File photoFile = null; 
      try{ 
       photoFile = createImageFile(); 
       takePictureIntent.putExtra("PhotoPath", mCM); 
      }catch(IOException ex){ 
       Log.e(TAG, "Image file creation failed", ex); 
      } 
      if(photoFile != null){ 
       mCM = "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, "Image Chooser"); 
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); 
    startActivityForResult(chooserIntent, FCR); 
    return true; 
} 

創建鏡像文件功能,在我們需要它來創建新的臨時文件上傳上面的代碼中提到。

private File createImageFile() throws IOException{ 
    @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "img_"+timeStamp+"_"; 
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
    return File.createTempFile(imageFileName,".jpg",storageDir); 
} 
+0

有git項目在鏈接 – Athul

+0

它適用於我。謝謝 !! – Anymore

+0

@Anymore:Upvote答案讓其他人輕鬆參考它 – Athul