拍摄照片并直接上传以响应WebView中的“选择文件”按钮

时间:2013-02-28 19:47:44

标签: java android android-intent android-webview android-camera

我正在使用WebView作为一个简单的应用程序,让用户在网站上传图片。当用户点击我网站上表格的“选择文件”部分时,我当前会弹出“图像选择器”。

我宁愿让用户选择一个动作(相机,图库,文件管理器)。当用户使用浏览器而不是WebView时,这种情况已经发生。我该怎么做?

我现有的代码如下:

package com.example.test;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class MainActivity extends Activity {


private WebView mWebView;

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;

    }
}

protected class MyWebChromeClient extends WebChromeClient {
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {  

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

       public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
           openFileChooser(uploadMsg);
       }                   

       public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
           openFileChooser(uploadMsg);
       }      
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("http://test.com");
    mWebView.setWebViewClient(new HelloWebViewClient());
    mWebView.setWebChromeClient(new MyWebChromeClient());
    Button newButton = (Button)findViewById(R.id.button1);
    newButton.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
            MainActivity.this.mWebView.loadUrl("http://test.com");
           }});
}

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView webview, String url){
        webview.loadUrl(url);
        return true;
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK)&& mWebView.canGoBack()){
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
}

1 个答案:

答案 0 :(得分:0)

尝试code by Joe S,它对我有用,现在我可以在文件选择器对话框中将相机与画廊一起使用。但是,你从相机拍照,它没有上传。我将继续努力并相应地更新它。