1

我只是喜欢webview无法点击上传文件按钮,但现在问题是在webview中点击上传文件按钮后,看不到相机,只能选择floder文件,如果在浏览器中打开网站,我可以看到(使用)相机并选择文件夹文件。

我的手机版本是 android 4.1 。

我的代码如下,请帮助,Tq。

package com.mypackage123;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
    Context mContext;
    WebView mWebView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.loadUrl("http://www.mywebsite12345.com");
        mWebView.setWebChromeClient(new CustomWebChromeClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new MyWebViewClient());

    }

    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 CustomWebChromeClient extends WebChromeClient {


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


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


        //Android 3.0
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");
            startActivityForResult(
                    Intent.createChooser(i, "Image Browser"),
                    FILECHOOSER_RESULTCODE);
        }




    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

        view.loadUrl(url);
        return true;
        }
        }



}
4

1 回答 1

0

声明这个变量:

public int TAKE_PICTURE = 1;
public Uri picUri;

在函数 openfilechooser() 中,用这个替换 openfilechooser(uploadMsg)

    mUploadMessage = uploadMsg;
    Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
    Calendar cal = Calendar.getInstance();
    cal.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
    File photo = new File(Environment.getExternalStorageDirectory(), sdf.format(cal.getTime()) +".jpg");
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    picUri = Uri.fromFile(photo);
    startActivityForResult(cameraIntent, TAKE_PICTURE);

不要忘记导入这个,

import java.util.Calendar;
import java.lang.Object;
import java.text.SimpleDateFormat;
import java.io.File;
import android.os.Environment;
import android.provider.MediaStore;

我只想知道,如何使用 json/javascript 和 php 将此图像发送到服务器?时间

于 2013-06-14T04:34:04.080 回答