【问题标题】:Open Filechooser after Permission Granted in Android在 Android 中授予权限后打开文件选择器
【发布时间】:2020-08-11 11:06:26
【问题描述】:

我正在使用以下代码检查当用户在 android webview 中单击我网站上的输入文件字段时是否已授予相机和存储权限:

 public boolean file_permission(){
         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.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);

            return false;
        }else{
            return true;
        }
    }

  class MyWebChromeClient extends WebChromeClient {

            // Handling onshow file chooser
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {

                if(file_permission() && Build.VERSION.SDK_INT >= 21) {

                    file_path = filePathCallback;
                    Intent takePictureIntent = null;

                        takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
                            File photoFile = null;
                            try {
                                photoFile = create_image();
                                takePictureIntent.putExtra("PhotoPath", cam_file_data);
                            } catch (IOException ex) {
                                Log.e(TAG, "Image file creation failed", ex);
                            }
                            if (photoFile != null) {
                                cam_file_data = "file:" + photoFile.getAbsolutePath();
                                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                            } else {
                                cam_file_data = null;
                                takePictureIntent = null;
                            }
                        }

                    Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
                    contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
                    contentSelectionIntent.setType(file_type);

                    Intent[] intentArray;

                    intentArray = new Intent[]{takePictureIntent};

                    Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
                    chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
                    chooserIntent.putExtra(Intent.EXTRA_TITLE, "Select Photo for yourPrint");
                    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
                    startActivityForResult(chooserIntent, file_req_code);
                    return true;

                }  else {
                    return false;
                }
            }
        }

当通过file_permission()检查权限时,会出现接受权限对话框。但是一旦用户接受了权限,用户需要再次点击输入文件字段以显示文件选择器。我希望在用户接受权限后再次自动单击最初触发 onShowFileChooser 的输入字段,以便文件选择器自动打开。

我曾尝试使用onRequestPermissionsResult 和 javascript 在使用此代码javascript:jQuery('.file-input-field').trigger('click') 接受权限后触发对输入字段的点击,但是,我不想使用这种方式,因为 chrome 会阻止对具有类型文件的输入字段的编程点击安全目的。

如何做到这一点?

【问题讨论】:

    标签: java android android-webview webchromeclient


    【解决方案1】:

    你必须像这样在 java/kotlin 中重置这个变量:

    filePathCallback.onReceiveValue(null)
    filePathCallback = null
    

    如果 filePathCallback 不为​​ null onShowFileChooser 将不会触发

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-06
      • 2011-09-07
      • 2019-06-18
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多