简体   繁体   English

Google 驱动器文件上传在 Android Webview 中不起作用

[英]Google drive file upload not working in Android Webview

I'm working on uploading file to WebView.我正在将文件上传到 WebView。 Local files work well.本地文件运行良好。 But files from google drive do not work.但是来自谷歌驱动器的文件不起作用。 URI information of local file is not empty.(not null)本地文件的URI信息不为空。(不为空)

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

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

This is my code.这是我的代码。

...
       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"));


...


I dont know the reason that happens.我不知道发生这种情况的原因。 Maybe do I have to check the webpage??也许我必须检查网页?

please help me... :(请帮我... :(

I saved the google drive file locally and send that local uri to webview.我在本地保存了 google 驱动器文件并将该本地 uri 发送到 webview。

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

The savedLocalFile() function returns a File stored in local(or external) storage. savedLocalFile() function 返回存储在本地(或外部)存储中的File

If you have any better ideas, please give leave comments.如果您有更好的想法,请留下评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM