AndroidWebview的onShowFileChooser方法无法显示结果。
创始人
2024-10-12 02:33:45
0

这个问题的解决方法是在onActivityResult方法中使用手动触发的onShowFileChooser回调。解决步骤如下:

  1. 确保你添加了以下两个权限:

  2. 添加以下代码到WebChromeClient的onShowFileChooser:

    ValueCallback mUploadMessage; String mCameraPhotoPath;

    public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { // Ensure that there's no existing callback if (mUploadMessage != null) { mUploadMessage.onReceiveValue(null); } mUploadMessage = filePathCallback;

     // Create camera intent
     Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
     if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
         // Create a file to store the image
         File photoFile = null;
         try {
             photoFile = createImageFile();
             cameraIntent.putExtra("PhotoPath", mCameraPhotoPath);
         } catch (IOException ex) {
             // Error occurred while creating the File
             Log.e(TAG, "Unable to create Image File", ex);
         }
    
         // Continue only if the File was successfully created
         if (photoFile != null) {
             mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
             cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
         } else {
             cameraIntent = null;
         }
     }
    
     // Create file chooser intent
     Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
     intent.addCategory(Intent.CATEGORY_OPENABLE);
     intent.setType("*/*");
    
     // Create file chooser intent
     Intent chooserIntent = Intent.createChooser(intent, "选择文件");
    
     // Add the camera options
     if (cameraIntent != null) {
         chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { cameraIntent });
     }
    
     // Launch the file chooser
     startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);
     return true;
    

    }

  3. 添加以下代码到Activity的onActivityResult:

    @Override public void onActivityResult (int requestCode, int resultCode, Intent data) { if (requestCode != INPUT_FILE_REQUEST_CODE || mUploadMessage == null) { super.onActivityResult(requestCode, resultCode, data); return; }

     Uri[] results = null;
    
     // Check that the response is a good one
     if (resultCode == Activity.RESULT_OK) {
         if (data == null) {
             // If there is not data, then we may have taken a photo
             if (mCameraPhotoPath != null) {
                 results = new Uri[] { Uri.parse(mCameraPhotoPath) };
             }
         } else {
             String dataString = data.getDataString();
             if (dataString != null) {
                 results = new Uri[] { Uri.parse(dataString) };
             }
         }
     }
    
     mUploadMessage.onReceiveValue(results);
     mUploadMessage = null;
    

    }

  4. 最后,定义输入文件请求代码。

    private static final int INPUT_FILE_REQUEST_CODE = 1;

通过这些步骤,您可以成功处理Android WebView onShowFileChooser方法无法显示结果的问题。

相关内容

热门资讯

这一现象值得深思!we pok... 这一现象值得深思!we poker辅助器v3.3,impoker辅助,法门教程(真的有挂)-哔哩哔哩...
透视好牌!德普之星有辅助软件吗... 透视好牌!德普之星有辅助软件吗,德普之星怎么设置埋牌(透视)确实有挂(有挂教学)-哔哩哔哩1、德普之...
今日!广东雀神智能插件需要付费... 今日!广东雀神智能插件需要付费吗,小程序微乐斗地主辅助,办法教程(有挂方略)-哔哩哔哩1、广东雀神智...
透视透视挂!德普之星透视辅助软... 透视透视挂!德普之星透视辅助软件是真的吗,德普之星透视辅助软件(透视)竟然有挂(有挂教学)-哔哩哔哩...
据报道!天天贵阳破解版,wep... 据报道!天天贵阳破解版,wepoker插件功能辅助器,方式教程(果真有挂)-哔哩哔哩;在进入wepo...
透视中牌率!德普之星辅助器ap... 透视中牌率!德普之星辅助器app,德普之星的辅助工具介绍(透视)竟然是真的挂(有人有挂)-哔哩哔哩1...
截至发稿!丰城双剑辅助器,小程... 截至发稿!丰城双剑辅助器,小程序雀神雀神智能插件,总结教程(详细教程)-哔哩哔哩1、下载好小程序雀神...
透视脚本!德普辅助器怎么用,德... 透视脚本!德普辅助器怎么用,德普之星透视软件免费入口官网(透视)一直是有挂(有挂解密)-哔哩哔哩1、...
为了进一步!多乐破解游戏盒子破... 为了进一步!多乐破解游戏盒子破解版,微乐小程序自建房辅助下载,机巧教程(有挂秘笈)-哔哩哔哩1、微乐...
透视脚本!德普之星怎么开辅助,... 透视脚本!德普之星怎么开辅助,德普之星怎么开辅助(透视)竟然真的是有挂(有挂功能)-哔哩哔哩一、德普...