图像从android中的Web视图上传

时间:2013-10-22 09:51:30

标签: android image android-webview

我使用网页视图创建了一个Android应用程序,以下是我的代码

webView = (WebView) findViewById(R.id.webView1); 
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.setWebViewClient(new WebViewClient()); 
    webView.loadUrl("http://aamirarif.com/tomnjoe/login.php");

现在,问题是在我的网站上有一个上传图像的选项,当我从android web视图打开它并且日志cat中没有显示错误时,该选项无效。我的更新代码是

public class WebViewActivity extends Activity {
private WebView webView;

private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    webView = (WebView) findViewById(R.id.webView1); 
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAllowFileAccess(true);

    webView.setWebViewClient(new WebViewClient()); 
    webView.loadUrl("http://aamirarif.com/tomnjoe/login.php");

}


@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;

}
}

class MyWebChromeClient extends WebChromeClient {
// The undocumented magic method override
// Eclipse will swear at you if you try to put @Override here
public void openFileChooser(ValueCallback<Uri> uploadMsg) {

    mUploadMessage = uploadMsg;
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("image/*");
    WebViewActivity.this.startActivityForResult(
            Intent.createChooser(i, "Image Browser"),
            FILECHOOSER_RESULTCODE);
}
}

 }

1 个答案:

答案 0 :(得分:0)

试一下

在代码中添加webView = (WebView) findViewById(R.id.webView1);之后的以下代码。

代码是:

webView = new WebView(this);