Android webview上传文件和相机

时间:2013-04-04 08:16:09

标签: android file upload webview camera

我只是单独浏览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;
        }
        }



}

1 个答案:

答案 0 :(得分:0)

声明这个var:

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将此图像发送到服务器? TQ