【问题标题】:"Choose File" button did not work after I changed permission更改权限后“选择文件”按钮不起作用
【发布时间】:2020-01-20 12:13:56
【问题描述】:

您好,我的项目需要我使用 webview 和摄像头。我在 HTML 中实现了代码,当按下“选择文件”按钮时,webview 将让我选择是使用相机拍摄照片还是从我的文件管理器上传文件。但我不想要文件选项,我只想在按下按钮后立即启动相机。这是它要求允许使用我的相机和外部存储的代码

public void get_file(){
    String[] perms = {/*Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, */Manifest.permission.CAMERA};

    //Checking for CAMERA Permissions first
    //if (ASWP_CAMUPLOAD && check_permission(3)) {
        //ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, file_perm);

    //Checking for storage permission to write images for upload
    if (ASWP_FUPLOAD && ASWP_CAMUPLOAD && !check_permission(2) && !check_permission(3)) {
        ActivityCompat.requestPermissions(MainActivity.this, perms, file_perm);

    //Checking for WRITE_EXTERNAL_STORAGE permission
    } else if (ASWP_FUPLOAD && !check_permission(2)) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, file_perm);

    //Checking for CAMERA permissions
    } else if (ASWP_CAMUPLOAD && !check_permission(3)) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, file_perm);
    }
}
public boolean check_permission(int permission){
    switch(permission){
        case 1:
            return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;

        case 2:
            return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;

        case 3:
            return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;

    }
    return false;
}

这是我更改的代码

static boolean ASWP_FUPLOAD     = false;     //upload file from webview
static boolean ASWP_ONLYCAM     = true ;     //only use camera as input

我将上传文件从 webview 选项从 true 更改为 false。但奇怪的是,在我将其更改为 false 之后,按下按钮不再起作用。我做错了什么?如果我只想使用相机,我应该删除 public void get_file() 中的一些行吗? 希望给点建议,谢谢! 编辑:我发现一个代码是 ASWP_ONLYCAM,它只启用相机作为唯一的输入!但我不知道如何激活它! p.s.如果您需要更多信息,请告诉我

【问题讨论】:

  • 您好,欢迎来到 StackOverflow! check_permission() 方法到底是做什么的?能否提供一下代码?
  • @C0nverter 您好,非常抱歉!我添加了,请看一下!

标签: java android permissions camera


【解决方案1】:

如我所见,您使用的是Android SmartWebView

我想你用来自here (ProgramCreek) 的代码来支持自己,因为你的方法到目前为止是一样的。如果是这样,ASWP_FUPLOADASWP_CAMUPLOAD 都需要为 true,因为 ASWP_CAMUPLOAD 取决于 ASWP_FUPLOAD

然后,你需要进入MainActivity。在asw_view.setWebChromeClient() 内部应该有这些空白:

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,WebChromeClient.FileChooserParams fileChooserParams)

您的照片管理有逻辑。第一个兼容Android API 16+,第二个兼容21+。

现在,从 API 16+(第一种方法)开始,我看不到直接的解决方案。您可以使用来自this thread 的那个。第二种方法虽然很容易编辑。您有 if 语句验证 ASWP_CAMUPLOADASWP_FUPLOAD。在ASWP_CAMUPLOAD 验证结束时,你有这个:

if(ASWP_CAMUPLOAD) {
    //...
} else {
    intentArray = new Intent[0];
}

return false,所以文件上传本身应该被取消,相机上传将成为唯一的解决方案。

【讨论】:

  • 感谢您的回复!但是我从“现在,从 API 16+ 开始……”迷失了自己,我需要修改什么来解决我的问题?很抱歉我的编程能力不是很强>
  • 等我明白了,所以我应该在 ASWP_CAMUPLOAD 循环结束时返回 false 对吧?
  • openFileChooser() 是适用于旧 Android 版本的方法。 API 16 可能在 Android 4.x 左右。您可以尝试理解这里的代码以查看文件是如何检索的,并使用我为您提供的线程来修改此方法。
  • 是的,但请记住,这适用于 Android API 21+。 Lollipop 下的 Android 设备仍然可以在文件和相机之间进行选择。
  • 啊好吧,我先看一下,如果我不明白,有没有更好的联系方式?
猜你喜欢
  • 1970-01-01
  • 2013-07-17
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多