【问题标题】:Google drive file upload not working in Android Webview谷歌驱动器文件上传在 Android Webview 中不起作用
【发布时间】:2021-11-26 09:33:05
【问题描述】:

我正在将文件上传到 WebView。 本地文件运行良好。但是来自谷歌驱动器的文件不起作用。 本地文件的URI信息不为空。(非空)

本地文件:content://com.android.providers.downloads.documents/document/xxx

Google 云端硬盘文件:content://com.google.android.apps.docs.storage/document/xxx

这是我的代码。

...
       takeFileResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            int resultCode = result.getResultCode();


            if (resultCode == RESULT_OK) {
                Intent intent = result.getData();
                Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileChooserDialog.class.getSimpleName());
                if (fragment instanceof DialogFragment) {
                    ((DialogFragment) fragment).dismiss();
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    if (null == uploadValueCallBack) {
                        return;
                    }
                    if (intent != null) {
                        Uri uri = intent.getData();
                        intent.setData(uri);
                    }

                    uploadUris = WebChromeClient.FileChooserParams.parseResult(resultCode, intent);
                    uploadValueCallBack.onReceiveValue(uploadUris);
                    uploadValueCallBack = null;

                } else if (null != uploadMessage) {
                    Uri resultUri = (intent == null) ? null : intent.getData();
                    uploadMessage.onReceiveValue(resultUri);
                    uploadMessage = null;
                }

            }


        });
...


       Intent i = new Intent(Intent.ACTION_GET_CONTENT);
       i.addCategory(Intent.CATEGORY_OPENABLE);
                   
       String[] fileMimeTypes = {
                            "application/pdf",
                            "application/zip",
                            "image/*",
                    };
       
       i.setType("*/*");
       i.putExtra(EXTRA_MIME_TYPES, fileMimeTypes);
       takeFileResultLauncher.launch(Intent.createChooser(i, "File Chooser"));


...


我不知道发生这种情况的原因。 也许我必须检查网页??

请帮帮我... :(

【问题讨论】:

    标签: java android html file-upload android-webview


    【解决方案1】:

    我在本地保存了 google 驱动器文件并将该本地 uri 发送到 webview。

    Intent intent = new Intent();
    intent.setData(Uri.fromFile(savedLocalFile(uri));
    WebChromeClient.FileChooserParams.parseResult(resultCode, intent);
    

    savedLocalFile() 函数返回存储在本地(或外部)存储中的 File

    如果你有更好的想法,请给leave cmets。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      相关资源
      最近更新 更多