1、定义

    private ValueCallback<Uri&gt; mUploadMessage;
    public ValueCallback<Uri[]&gt; uploadMessage;
   private Uri imageUri;

2、webview 设置

     webView.setWebChromeClient(new WebChromeClient(){
            // For 3.0+ Devices (Start)
            // onActivityResult attached before constructor
            protected void openFileChooser(ValueCallback uploadMsg, String acceptType)
            {
                mUploadMessage = uploadMsg;
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("image/*");
                startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
            }



            // For Lollipop 5.0+ Devices
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
            {
                if (uploadMessage != null) {
//                    uploadMessage.onReceiveValue(null);
                    uploadMessage = null;
                }

                uploadMessage = filePathCallback;

                try
                {
                    takePhoto();
                } catch (ActivityNotFoundException e)
                {
                    uploadMessage = null;
                    Toast.makeText(getBaseContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
                    return false;
                }
                return true;
            }

            //For Android 4.1 only
            protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
            {
                mUploadMessage = uploadMsg;
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);
            }

            protected void openFileChooser(ValueCallback<Uri> uploadMsg)
            {
                mUploadMessage = uploadMsg;
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("image/*");
                startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
            }

        });

3、调用摄像机

    @NonNull
    private File createTakePhotoFile(){
        File imagePath = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),"zzwlnet_driver");
        if (!imagePath.exists()){
            imagePath.mkdir();
        }
        File file = new File(imagePath,SystemClock.currentThreadTimeMillis() + ".jpg");
        return file;
    }

    public void takePhoto(){

        //调用系统相册
//        Intent intentPhoto = new Intent(Intent.ACTION_PICK);
        Intent intentPhoto = new Intent(Intent.ACTION_GET_CONTENT);
        intentPhoto.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");

        //Environment.getExternalStorageDirectory().getPath()
        //调用系统相机
        File fileUri =  createTakePhotoFile();
        imageUri = Uri.fromFile(fileUri);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            Log.d("********name*****",getPackageName());
            imageUri = FileProvider.getUriForFile(this,getPackageName() + ".fileprovider",fileUri);
        }
        Intent intentCamera = new Intent();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            intentCamera.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        intentCamera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

        intentCamera.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);

        Intent chooser = new Intent(Intent.ACTION_CHOOSER);
        chooser.putExtra(Intent.EXTRA_TITLE,"上传照片");
        chooser.putExtra(Intent.EXTRA_INTENT,intentPhoto);
        chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,new Intent[]{intentCamera});
        startActivityForResult(chooser,REQUEST_SELECT_FILE);


    }

4、回调方法

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent)
    {

        Log.d("********resultCode*****", String.valueOf(resultCode));
        Log.d("*******requestCode*****", String.valueOf(requestCode));
        if (intent != null){
            Log.d("*******intent*****", String.valueOf(intent.getData()));
        }

        Log.d("*******SDK_INT*****", String.valueOf(Build.VERSION.SDK_INT));
        Log.d("*******LOLLIPOP*****", String.valueOf( Build.VERSION_CODES.LOLLIPOP));

        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Toast.makeText(getBaseContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show();
            return;
        }
        if (requestCode != REQUEST_SELECT_FILE) {
            return;
        }
        if (uploadMessage == null){

            return;
        }
        if (intent != null){
            uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
            uploadMessage = null;
            return;
        }

        Uri[] results = new Uri[]{imageUri};
        uploadMessage.onReceiveValue(results);
        uploadMessage = null;
    }

 

原文地址:https://blog.csdn.net/vickylizy/article/details/134663838

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_4795.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注