AndroidWebview的onShowFileChooser方法无法显示结果。
创始人
2024-10-12 02:34:40
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方法无法显示结果的问题。

相关内容

热门资讯

安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...