0

我将 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);
}
}
4

1 回答 1

0

试试Joe S 的代码,它对我有用,现在我可以在文件选择器对话框中将相机放在画廊旁边。但是,您从相机拍摄的照片不会上传。我会处理它并相应地更新它。

于 2013-03-31T04:20:30.817 回答