【问题标题】:WebView, input of type file, camera and imageWebView,输入类型文件,相机和图像
【发布时间】:2013-09-02 08:20:40
【问题描述】:

我已经在谷歌上搜索了很长一段时间......但我仍然找不到我的问题的真正答案。 问题是我需要一种方法让我的用户在按下 type="file" 的输入元素后能够选择从画廊上传文件还是直接从相机上传文件。 因此,如果那里有任何好的样本,请告诉我,或者如果你有样本可以让我看看。

提前致谢!

【问题讨论】:

    标签: android image input webview camera


    【解决方案1】:
    public void attachFileInput() {
    
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        ((Activity) mContext).startActivityForResult(
                    Intent.createChooser(i, "Image Choser"), 1);
    }
    

    此方法将在您的 JSInterface 中。 调用如下:

    $(".file").live('click', function() {
        mySJInterface.attachFileInput();
    });
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我正在创建一个使用带有 android webview 的输入标签上传文件的程序。顺便说一句,我不是安卓应用程序员。

      请查看以下参考资料:Android webview, openfilechooser terminationhttp://www.cnblogs.com/sipher/archive/2012/09/05/2672361.html

      步骤:

      1. 使用输入文件请求制作菜单以捕获照片 - Android webview, openfilechooser termination

      2. 但是图片没有发送到服务器并且修改了 imageUri 值Environment.getExternalStoragePublicDirectory 如此处所示 - http://www.cnblogs.com/sipher/archive/2012/09/05/2672361.html

      3. 所以我需要在 Manifest 中添加权限 WRITE_EXTERNAL_STORAGE。

        webView.setWebChromeClient(new WebChromeClient(){
            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg){
                this.openFileChooser(uploadMsg, "");
            }
        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType, String capture) {
            this.openFileChooser(uploadMsg, "");
        }
        
        @SuppressWarnings("unused")
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType) {
            final List<Intent> cameraIntents = new ArrayList<Intent>();
            final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File externalDataDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
            File cameraDataDir = new File(externalDataDir.getAbsolutePath()+File.separator+"browser-photos");
            cameraDataDir.mkdirs();
            String mCameraFilePath = cameraDataDir.getAbsolutePath()+File.separator+System.currentTimeMillis()+".jpg";
            imageUri = Uri.fromFile(new File(mCameraFilePath));
        
        final PackageManager packageManager = getPackageManager();
        final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
        for(ResolveInfo res : listCam) {
            final String packageName = res.activityInfo.packageName;
            final Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            intent.setPackage(packageName);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            cameraIntents.add(intent);
            }
        
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");               
        Intent chooserIntent = Intent.createChooser(i,"Image Chooser");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
        MainActivity.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
        }
        });
        webView.getSettings().setDomStorageEnabled(true);
        webView.loadUrl("..............");
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setWebViewClient(new WebViewClientClass());}
        

      现在。我可以上传带有输入标签的相机拍摄的照片。

      更新:You can look for alternate methods to solve the same issue

      【讨论】:

        猜你喜欢
        • 2015-03-24
        • 1970-01-01
        • 2011-02-20
        • 1970-01-01
        • 2014-12-05
        • 2018-12-27
        • 1970-01-01
        • 2020-04-17
        相关资源
        最近更新 更多