4

可能重复:
WebView 中的文件上传

我希望这个问题有一个简单的答案。我在我的应用程序中运行了这个 Android 代码:

package com.example.myfirstapp;

import android.webkit.WebViewClient;
import android.webkit.WebView;
import android.util.Log;

public class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            Log.d ("myfirstapp_tag", "shouldOver... loaded Url " + url  );
            return true;
    }
}

...但是当这个 HTML 被加载时...

<tr><td>
    <form method="post" action="/myphp.php" enctype="multipart/form-data">
            <label for="upload">Add Pic:</label><input type="file" name="file" id="file" />
            <input type="hidden" name="action" value="upload" />
            <input type="submit" name="submit" value="Upload" />
    </form>
</td></tr>

...表单显示有一个“选择文件”按钮,但是当我点击它时没有任何反应。我在这里做错了什么?

谢谢

4

1 回答 1

2

我在stackoverflow上找到了答案,但丢失了链接,所以这就是我必须做的......

在我的 MainActivity 课程中,我这样做......

...
import android.webkit.ValueCallback;
import android.net.Uri;

public class MainActivity extends Activity {
...
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE=1; 


@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {  
    if(requestCode==FILECHOOSER_RESULTCODE)  {  
        if (null == mUploadMessage) return;  
        Uri result = intent == null || resultCode != RESULT_OK ? null  : intent.getData();  
        mUploadMessage.onReceiveValue(result);  
        mUploadMessage = null;
    }  
}  

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new MyWebViewClient());

    webView.setWebChromeClient(
        new WebChromeClient() {   
            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {  
                mUploadMessage = uploadMsg;  
Log.d ("myfirstapp_tag", "1) MainActivity.java loaded openFileChooser" );
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
Log.d ("myfirstapp_tag", "2) MainActivity.java loaded openFileChooser" );                   
                i.addCategory(Intent.CATEGORY_OPENABLE);  
Log.d ("myfirstapp_tag", "3) MainActivity.java loaded openFileChooser" );
                i.setType("image/*");  
Log.d ("myfirstapp_tag", "4) MainActivity.java loaded openFileChooser" );
MainActivity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);
Log.d ("myfirstapp_tag", "5) MainActivity.java loaded openFileChooser" );                     
            }  
        }
    );  



    webView.loadUrl("http://example.com/EZC.php");
    setContentView(webView);
    Log.d ("myfirstapp_tag", "MainActivity.java loaded URL and set Content View." ); 
}   
 }
于 2012-09-23T23:27:45.873 回答