当前位置: 首页 > news >正文

android webview file标签点击弹出选择文件或拍照菜单


1. Web page html:
<input type="file" name="image" accept="image/*" id="fileChoosed">    


2.android manifest文件

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />


<uses-feature android:name="android.hardware.camera" android:required="true" />


3. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0px"
    android:paddingLeft="0px"
    android:paddingRight="0px"
    android:paddingTop="0px"
    android:background="#364150"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    tools:context="com.esbu.nec.bme.MainActivity">


    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />




    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleLarge"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>






4. MainActivity.java


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






        Log.v(TAG, TAG + " # onActivityResult # requestCode=" + requestCode + " # resultCode=" + resultCode);
        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == mUploadMessage)
                return;
            Uri result = intent == null || resultCode != Activity.RESULT_OK ? null : intent.getData();
            mUploadMessage.onReceiveValue(result);
            mUploadMessage = null;


        } else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5) {
            if (null == mUploadMessageForAndroid5)
                return;
            Uri result;


            if (intent == null || resultCode != Activity.RESULT_OK) {
                result = null;
            } else {
                result = intent.getData();
            }


            if (result != null) {
                Log.v(TAG, TAG + " # result.getPath()=" + result.getPath());
                mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
            } else {
                mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
            }
            mUploadMessageForAndroid5 = null;
        } else if (requestCode == REQUEST_GET_THE_THUMBNAIL) {
            if (resultCode == Activity.RESULT_OK) {
                File file = new File(mCurrentPhotoPath);
                Uri localUri = Uri.fromFile(file);
                Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri);
                mActivity.sendBroadcast(localIntent);


                Uri result = Uri.fromFile(file);
                mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
                mUploadMessageForAndroid5 = null;
            } else {


                File file = new File(mCurrentPhotoPath);
                Log.v(TAG, TAG + " # file=" + file.exists());
                if (file.exists()) {
                    file.delete();
                }
            }
        }




        super.onActivityResult(requestCode, resultCode, intent);
       // mWebView.onActivityResult(requestCode, resultCode, intent);
    }


   private Activity mActivity;
    @SuppressLint({"SetJavaScriptEnabled", "WrongViewCast"})
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Log.d(TAG, "onCreate: ##########################");






        mActivity = this;


        if(Build.VERSION.SDK_INT >=23 && (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]
                    {
                            Manifest.permission.CAMERA,
                            Manifest.permission.CAPTURE_VIDEO_OUTPUT,
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE
                    }, 1);
        }






        mWebView = (WebView) findViewById(R.id.webview);


        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);












        mWebView.requestFocusFromTouch();
        mWebView.setWebViewClient(new MyWebViewClient());




        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(progress);
                if (progress == 100) {
                    progressBar.setVisibility(mWebView.GONE);


                } else {
                    progressBar.setVisibility(mWebView.VISIBLE);
                }
            }


            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                Log.i("", "|> onPermissionRequest");


                MainActivity.this.runOnUiThread(new Runnable(){
                    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                    @Override
                    public void run() {
                        Log.i("", "|> onPermissionRequest run");
                        request.grant(request.getResources());
                    }// run
                });// MainActivity










            }// onPermissionRequest




            //3.0++
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                Log.d(TAG, "openFileChooser: 3.0++");
                openFileChooserImpl(uploadMsg);
            }


            //3.0--
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                Log.d(TAG, "openFileChooser: 3.0--");
                openFileChooserImpl(uploadMsg);
            }


            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                Log.d(TAG, "openFileChooser: ");
                openFileChooserImpl(uploadMsg);
            }


            // For Android > 5.0
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
                openFileChooserImplForAndroid5(uploadMsg);
                return true;
            }








            public void onPageFinished(WebView view, String url) { mWebView.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }




            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed(); // Ignore SSL certificate errors
            }


        });






       // mWebView.setListener(this, this);
        mWebView.loadUrl("your_app_url");












    }




    private String[] items = new String[]{"Choose Image","Take Photo"};
    private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) {
        Log.d(TAG, "openFileChooserImpl: 1");


        mUploadMessage = uploadMsg;
        Log.d(TAG, "openFileChooserImpl: 2");
        new AlertDialog.Builder(mActivity)
                .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (items[which].equals(items[0])) {
                            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                            i.addCategory(Intent.CATEGORY_OPENABLE);
                            i.setType("image/*");
                            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
                        } else if (items[which].equals(items[1])) {
                            dispatchTakePictureIntent();
                        }
                        dialog.dismiss();
                    }
                })
                .setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        Log.v(TAG, TAG + " # onCancel");
                        mUploadMessage = null;
                        dialog.dismiss();
                    }})
                .show();
        Log.d(TAG, "openFileChooserImpl: 3");
    }


    private void openFileChooserImplForAndroid5(ValueCallback<Uri[]> uploadMsg) {
        mUploadMessageForAndroid5 = uploadMsg;
        Log.d(TAG, "openFileChooserImplForAndroid5: 1");
        new AlertDialog.Builder(mActivity)
                .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (items[which].equals(items[0])) {
                            Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
                            contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
                            contentSelectionIntent.setType("image/*");


                            Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
                            chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
                            chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");


                            startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5);
                        } else if (items[which].equals(items[1])) {
                            dispatchTakePictureIntent();
                        }
                        dialog.dismiss();
                    }
                })
                .setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        Log.v(TAG, TAG + " # onCancel");
                        //important to return new Uri[]{}, when nothing to do. This can slove input file wrok for once.
                        //InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
                        mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
                        mUploadMessageForAndroid5 = null;
                        dialog.dismiss();
                    }}).show();
        Log.d(TAG, "openFileChooserImplForAndroid5: 2");
    }


    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(mActivity.getPackageManager()) != null) {
//            File file = new File(createImageFile());
            Uri imageUri = null;
            try {
		// *** the code may different if target android v24+
                imageUri = Uri.fromFile(createImageFile());
            } catch (IOException e) {
                e.printStackTrace();
            }
            //temp sd card file
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(takePictureIntent, REQUEST_GET_THE_THUMBNAIL);
        }
    }


    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/don_test/");
        if (!storageDir.exists()) {
            storageDir.mkdirs();
        }
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );


        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }








    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MainActivity.this,"permission granted", Toast.LENGTH_SHORT).show();
                // perform your action here


            } else {
                Toast.makeText(MainActivity.this,"permission not granted", Toast.LENGTH_SHORT).show();
            }
        }


    }


    private WebView mWebView;


    @SuppressLint("NewApi")
    @Override
    protected void onResume() {
        super.onResume();
        mWebView.onResume();
        // ...
    }


    @SuppressLint("NewApi")
    @Override
    protected void onPause() {
        mWebView.onPause();
        // ...
        super.onPause();
    }


    @Override
    protected void onDestroy() {
      //  mWebView.onDestroy();
        // ...
        super.onDestroy();
    }






    @Override
    public void onBackPressed() {
      //  if (!mWebView.onBackPressed()) { return; }
        // ...
        super.onBackPressed();
    }


...


相关文章:

  • AzureAD 错误信息Access token validation failure
  • android webview实现拍照
  • lens flare:镜头光晕
  • SQL批处理 导入excel数据到表
  • opengl粒子系统的渲染方式
  • html5 canvas上传图片后预览
  • ZBuffer裁剪planar shadow
  • 3D几何流水线之模型变换
  • html5 canvas 加载图片错误 SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement'
  • RGBA模式人物换色的实现
  • javascript实现QR code扫描
  • android webview旋转屏幕导致页面重新加载问题
  • FLEX实践—自创相册
  • Nebula3的渲染线程插件(Render Thread Plugin)
  • android webview 遇到android.os.FileUriExposedException错误
  • ES6指北【2】—— 箭头函数
  • [LeetCode] Wiggle Sort
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • cookie和session
  • ESLint简单操作
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • niucms就是以城市为分割单位,在上面 小区/乡村/同城论坛+58+团购
  • php ci框架整合银盛支付
  • Python 基础起步 (十) 什么叫函数?
  • React-Native - 收藏集 - 掘金
  • Redis 中的布隆过滤器
  • v-if和v-for连用出现的问题
  • 更好理解的面向对象的Javascript 1 —— 动态类型和多态
  • 排序算法之--选择排序
  • 用 Swift 编写面向协议的视图
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • ​MySQL主从复制一致性检测
  • # C++之functional库用法整理
  • #Linux(权限管理)
  • #每日一题合集#牛客JZ23-JZ33
  • (附源码)spring boot火车票售卖系统 毕业设计 211004
  • (黑客游戏)HackTheGame1.21 过关攻略
  • (推荐)叮当——中文语音对话机器人
  • (一)Neo4j下载安装以及初次使用
  • (已更新)关于Visual Studio 2019安装时VS installer无法下载文件,进度条为0,显示网络有问题的解决办法
  • (转)iOS字体
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • *(长期更新)软考网络工程师学习笔记——Section 22 无线局域网
  • ***监测系统的构建(chkrootkit )
  • ../depcomp: line 571: exec: g++: not found
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .NET 5种线程安全集合
  • .NET 8.0 中有哪些新的变化?
  • .Net core 6.0 升8.0
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution
  • .NET Framework 的 bug?try-catch-when 中如果 when 语句抛出异常,程序将彻底崩溃
  • @Autowired多个相同类型bean装配问题