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方法无法显示结果的问题。

相关内容

热门资讯

总算清楚!hhpoker到底可... 总算清楚!hhpoker到底可以作弊码,pokemmo手机脚本辅助器,2025新版教程(有挂方法);...
记者爆料!约局吧能不能开挂,w... 此外,数据分析德州(约局吧能不能开挂)辅助神器app还具备辅助透视行为开挂功能,通过对客户约局吧能不...
技术分享!wepoker辅助器... 技术分享!wepoker辅助器官方,sohoo辅助,软件教程(有挂技巧)准备好在wepoker辅助器...
信息共享!cloudpoker... 这是一款非常优秀的cloudpoker怎么开挂 ia辅助检测软件,能够让你了解到cloudpoker...
推荐十款!wepoker透视脚... 推荐十款!wepoker透视脚本视频,hhpoker买挂,安装教程(有挂神器);科技安装教程;136...
科普攻略!aapoker辅助怎... 科普攻略!aapoker辅助怎么用,hhpoker有后台操作吗,安装教程(有挂技巧)1、点击下载安装...
玩家必看教程!竞技联盟破解版最... 玩家必看教程!竞技联盟破解版最新版,云扑克有透视吗,技巧教程(有挂辅助)这是由厦门游乐互动科技有限公...
信息共享!wepoker怎么设... 1、信息共享!wepoker怎么设置房间,wepoker辅助真的假的,揭秘教程(有挂攻略);详细教程...
教程辅助!hhpoker是正规... 教程辅助!hhpoker是正规的吗,约局吧怎么看有没有挂,技巧教程(有挂透明);实战中需综合运用上述...
一分钟快速了解!哈糖大菠萝有挂... 《一分钟快速了解!哈糖大菠萝有挂吗5个常用方法,agpoker辅助,2025版教程(有挂技巧)》 哈...