Android webview上传图片(调起相册/相机上传)

这篇具有很好参考价值的文章主要介绍了Android webview上传图片(调起相册/相机上传)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

概述

默认情况

WebView 不支持<input type=file>,WebView 点击没有反应。

兼容

重写 webview 的 webchromeClient 中的 openFileChooser 方法。Android 版本的多样性,就理所当然的各种兼容。

  // For Android < 3.0
    public void openFileChooser(ValueCallback uploadMsg) {
        openFileChooser(uploadMsg, "");
    }

    //For Android 3.0 - 4.0
    public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
    }

    // For Android 4.0 - 5.0
    public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
        openFileChooser(uploadMsg, acceptType);
    }

    // For Android > 5.0
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {
        return true;
    }

具体代码实现

网上也有很多实现方式,这边记录一下自己用到的一种
webview 调用时,弹出本地弹框,选择(相册/相机/取消),选择相册跳转到文件选择页面、选择相机注意权限配置,返回照片要做压缩、选择取消注意给webview一个空回应,要不点击没有反应。

  • ZpWebChromeClient
  • public class ZpWebChromeClient extends WebChromeClient {
    
        private OpenFileChooserCallBack mOpenFileChooserCallBack;
    
        // For Android < 3.0
        public void openFileChooser(ValueCallback uploadMsg) {
            openFileChooser(uploadMsg, "");
        }
    
        //For Android 3.0 - 4.0
        public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
            if (mOpenFileChooserCallBack != null) {
                mOpenFileChooserCallBack.openFileChooserCallBack(uploadMsg, acceptType);
            }
        }
    
        // For Android 4.0 - 5.0
        public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
            openFileChooser(uploadMsg, acceptType);
        }
    
        // For Android > 5.0
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {
            if (mOpenFileChooserCallBack != null) {
                mOpenFileChooserCallBack.showFileChooserCallBack(filePathCallback, fileChooserParams);
            }
            return true;
        }
    
        public void setOpenFileChooserCallBack(OpenFileChooserCallBack callBack) {
            mOpenFileChooserCallBack = callBack;
        }
    
        public interface OpenFileChooserCallBack {
    
            void openFileChooserCallBack(ValueCallback uploadMsg, String acceptType);
    
            void showFileChooserCallBack(ValueCallback filePathCallback, FileChooserParams fileChooserParams);
        }
    
    }
  • ZpWebView
  • public class ZpWebView extends WebView {
    
        private ZpWebChromeClient webChromeClient;
    
        public ZpWebView(Context context) {
            super(context);
            initWebView();
        }
    
        public ZpWebView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initWebView();
        }
    
        public ZpWebView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initWebView();
        }
    
        private void initWebView() {
            webChromeClient = new ZpWebChromeClient();
            setWebChromeClient(webChromeClient);
    
            WebSettings webviewSettings = getSettings();
            // 不支持缩放
            webviewSettings.setSupportZoom(false);
            // 自适应屏幕大小
            webviewSettings.setUseWideViewPort(true);
            webviewSettings.setLoadWithOverviewMode(true);
            String cacheDirPath = getContext().getFilesDir().getAbsolutePath() + "cache/";
            webviewSettings.setAppCachePath(cacheDirPath);
            webviewSettings.setAppCacheEnabled(true);
            webviewSettings.setDomStorageEnabled(true);
            webviewSettings.setAllowFileAccess(true);
            webviewSettings.setAppCacheMaxSize(1024 * 1024 * 8);
            webviewSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        }
    
        public void setOpenFileChooserCallBack(ZpWebChromeClient.OpenFileChooserCallBack callBack) {
            webChromeClient.setOpenFileChooserCallBack(callBack);
        }
    }
  • MainActivity
  • public class MainActivity extends AppCompatActivity {
    
        public static final int REQUEST_SELECT_FILE_CODE = 100;
        private static final int REQUEST_FILE_CHOOSER_CODE = 101;
        private static final int REQUEST_FILE_CAMERA_CODE = 102;
        // 默认图片压缩大小(单位:K)
        public static final int IMAGE_COMPRESS_SIZE_DEFAULT = 400;
        // 压缩图片最小高度
        public static final int COMPRESS_MIN_HEIGHT = 900;
        // 压缩图片最小宽度
        public static final int COMPRESS_MIN_WIDTH = 675;
    
        private ValueCallback mUploadMsg;
        private ValueCallback mUploadMsgs;
        // 相机拍照返回的图片文件
        private File mFileFromCamera;
        private File mTakePhotoFile;
        private BottomSheetDialog selectPicDialog;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            initView();
        }
    
        private void initView() {
            ZpWebView webView = findViewById(R.id.webview);
            webView.setOpenFileChooserCallBack(new ZpWebChromeClient.OpenFileChooserCallBack() {
                @Override
                public void openFileChooserCallBack(ValueCallback uploadMsg, String acceptType) {
                    mUploadMsg = uploadMsg;
                    showSelectPictrueDialog(0, null);
                }
    
                @Override
                public void showFileChooserCallBack(ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
                    if (mUploadMsgs != null) {
                        mUploadMsgs.onReceiveValue(null);
                    }
                    mUploadMsgs = filePathCallback;
                    showSelectPictrueDialog(1, fileChooserParams);
                }
            });
        }
    
        /**
         * 选择图片弹框
         */
        private void showSelectPictrueDialog(final int tag, final WebChromeClient.FileChooserParams fileChooserParams) {
            selectPicDialog = new BottomSheetDialog(this, R.style.Dialog_NoTitle);
            selectPicDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    if (mUploadMsgs != null) {
                        mUploadMsgs.onReceiveValue(null);
                        mUploadMsgs = null;
                    }
                }
            });
            View view = LayoutInflater.from(this).inflate(R.layout.dialog_bottom_select_pictrue, null);
            // 相册
            TextView album = view.findViewById(R.id.tv_select_pictrue_album);
            // 相机
            TextView camera = view.findViewById(R.id.tv_select_pictrue_camera);
            // 取消
            TextView cancel = view.findViewById(R.id.tv_select_pictrue_cancel);
    
            album.setOnClickListener(new View.OnClickListener() {
                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void onClick(View view) {
                    if (tag == 0) {
                        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                        i.addCategory(Intent.CATEGORY_OPENABLE);
                        i.setType("*/*");
                        startActivityForResult(Intent.createChooser(i, "File Browser"), REQUEST_FILE_CHOOSER_CODE);
                    } else {
                        try {
                            Intent intent = fileChooserParams.createIntent();
                            startActivityForResult(intent, REQUEST_SELECT_FILE_CODE);
                        } catch (ActivityNotFoundException e) {
                            mUploadMsgs = null;
                        }
                    }
                }
            });
            camera.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    takeCameraPhoto();
                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    selectPicDialog.dismiss();
                }
            });
    
            selectPicDialog.setContentView(view);
            selectPicDialog.show();
        }
    
        public void takeCameraPhoto() {
            if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
                Toast.makeText(this, "设备无摄像头", Toast.LENGTH_SHORT).show();
                return;
            }
    
            String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
            
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            mFileFromCamera = new File(filePath, System.nanoTime() + ".jpg");
    
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTakePhotoFile));
                startActivityForResult(intent, REQUEST_FILE_CAMERA_CODE);
            } else {
                try {
                    mTakePhotoFile = File.createTempFile("Zp" + System.nanoTime(), ".jpg", filePath);
                    Uri contentUri = FileProvider.getUriForFile(InnerBrowserActivity.this,
                            "com.zp.demo.ZpFileProvider", mTakePhotoFile);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
                            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    startActivityForResult(intent, REQUEST_FILE_CAMERA_CODE);
                } catch (IOException e) {
                    
                }
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            switch (requestCode) {
                case REQUEST_SELECT_FILE_CODE:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        if (mUploadMsgs == null) {
                            return;
                        }
                        mUploadMsgs.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
                        mUploadMsgs = null;
                    }
                    break;
                case REQUEST_FILE_CHOOSER_CODE:
                    if (mUploadMsg == null) {
                        return;
                    }
                    Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
                    mUploadMsg.onReceiveValue(result);
                    mUploadMsg = null;
                    break;
                case REQUEST_FILE_CAMERA_CODE:
                    takePictureFromCamera();
                    break;
            }
        }
    
        /**
         * 处理相机返回的图片
         */
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        private void takePictureFromCamera() {
            if (mFileFromCamera != null && mFileFromCamera.exists()) {
                String filePath = mFileFromCamera.getAbsolutePath();
                // 压缩图片到指定大小
                File imgFile = ZpImageUtils.compressImage(this, filePath, COMPRESS_MIN_WIDTH, COMPRESS_MIN_HEIGHT, IMAGE_COMPRESS_SIZE_DEFAULT);
    
                Uri localUri = Uri.fromFile(imgFile);
                Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri);
                this.sendBroadcast(localIntent);
                Uri result = Uri.fromFile(imgFile);
    
                if (mUploadMsg != null) {
                    mUploadMsg.onReceiveValue(Uri.parse(filePath));
                    mUploadMsg = null;
                }
                if (mUploadMsgs != null) {
                    mUploadMsgs.onReceiveValue(new Uri[]{result});
                    mUploadMsgs = null;
                }
            } else {
                if (mUploadMsg != null) {
                    mUploadMsg.onReceiveValue(null);
                    mUploadMsg = null;
                }
                if (mUploadMsgs != null) {
                    mUploadMsgs.onReceiveValue(null);
                    mUploadMsgs = null;
                }            
            }
        }
    }

    备注:代码不规范的地方,自己抽取一下,这里就不做抽取了。

  • Github 地址

  • WebViewUploadFileDemo文章来源地址https://www.toymoban.com/news/detail-420597.html

到了这里,关于Android webview上传图片(调起相册/相机上传)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包