2011-02-09 18 views
5

我應該在應用程序的webview中顯示一個網頁。該頁面包含一個html表單,其中一個字段是文件。如此這般像...Android webview,文件輸入字段filechoos不顯示

<input type="file" name="file"> 

如果我在瀏覽器中打開頁面,然後按選擇文件按鈕,文件選擇彈出,一切都很好,但是當我按下選擇在網頁視圖文件按鈕什麼也沒有發生:/

任何想法如何使這項工作?

+0

您正在使用哪種Android版本?我相信之前FroYo(2.2)內置的Android瀏覽器不支持文件上傳:http://code.google.com/p/android/issues/detail?id = 2519(從2.2它應該工作雖然,在我的N1運行2.2中工作正常)。 – 2011-02-09 14:49:24

+0

@Charlie Collins我正在2.1,2.2,2.3上試試這個,如果它在任何這些工作上都可以。即使在2.1(Galaxy S)上,它在瀏覽器中也可以正常工作,但我需要在WebView中使用自定義應用程序。你是說它在你的應用程序中使用2.2上的WebView可以正常工作嗎? – m0s 2011-02-09 15:02:25

回答

6

WebView默認情況下不打開文件選擇器。但是可以做到這一點。 WebChromeClient隱藏方法openFileChooser,需要重寫以彈出文件選擇器,然後將結果返回給WebView。根據大師們不應該使用Android SDK的隱藏方法,所以這不是一個好的解決方案,並且可能不應該用於企業應用程序。不過Android的股票瀏覽器完全是這樣的。更多的信息我如何覆蓋這種方法是在我的這個question。如果有人需要這個消息來源讓我知道我發佈在某個地方。

2

是否可以通過webview上傳文件?

此功能在1.3.0和更新的版本。對於舊版本的應用程序,是的,這是可能的,但你需要添加一些額外的代碼,使其工作。將以下代碼添加到您的AndroidManifest.xml文件中。將它放置在其他使用權限元素下方:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

將此java類MediaUtility.java複製/粘貼到項目中。將其放入com/robotemplates/webviewapp/utility目錄中。

打開MainFragment.java在片段包。查找的RenderView()方法和替換「webView.setWebChromeClient(新WebChromeClient());」通過下面的代碼:

webView.setWebChromeClient(new WebChromeClient() 
{ 
    public void openFileChooser(ValueCallback<Uri> filePathCallback) 
    { 
     mFilePathCallback4 = filePathCallback; 
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 
     intent.setType("*/*"); 
     startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); 
    } 

    public void openFileChooser(ValueCallback filePathCallback, String acceptType) 
    { 
     mFilePathCallback4 = filePathCallback; 
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 
     intent.setType("*/*"); 
     startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); 
    } 

    public void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) 
    { 
     mFilePathCallback4 = filePathCallback; 
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 
     intent.setType("*/*"); 
     startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); 
    } 

    @Override 
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) 
    { 
     mFilePathCallback5 = filePathCallback; 
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 
     intent.setType("*/*"); 
     startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); 
     return true; 
    } 
}); 

最後添加以下代碼某處MainFragment對象內(例如這條線「的私人布爾mLocal =假;」):

private static final int REQUEST_FILE_PICKER = 1; 
private ValueCallback<Uri> mFilePathCallback4; 
private ValueCallback<Uri[]> mFilePathCallback5; 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent intent) 
{ 
    if(requestCode==REQUEST_FILE_PICKER) 
    { 
     if(mFilePathCallback4!=null) 
     { 
      Uri result = intent==null || resultCode!=Activity.RESULT_OK ? null : intent.getData(); 
      if(result!=null) 
      { 
       String path = MediaUtility.getPath(getActivity(), result); 
       Uri uri = Uri.fromFile(new File(path)); 
       mFilePathCallback4.onReceiveValue(uri); 
      } 
      else 
      { 
       mFilePathCallback4.onReceiveValue(null); 
      } 
     } 
     if(mFilePathCallback5!=null) 
     { 
      Uri result = intent==null || resultCode!=Activity.RESULT_OK ? null : intent.getData(); 
      if(result!=null) 
      { 
       String path = MediaUtility.getPath(getActivity(), result); 
       Uri uri = Uri.fromFile(new File(path)); 
       mFilePathCallback5.onReceiveValue(new Uri[]{ uri }); 
      } 
      else 
      { 
       mFilePathCallback5.onReceiveValue(null); 
      } 
     } 

     mFilePathCallback4 = null; 
     mFilePathCallback5 = null; 
    } 
} 

不要忘了添加必要的進口‘進口android.webkit.ValueCallback;’,‘進口com.robotemplates.webviewapp.utility.MediaUtility;’,「導入java.io 。文件;」。如果仍有問題,請嘗試在Proguard腳本中添加規則:https://code.google.com/p/android/issues/detail?id=62220#c120