简体   繁体   中英

Google drive file upload not working in Android Webview

I'm working on uploading file to WebView. Local files work well. But files from google drive do not work. URI information of local file is not empty.(not null)

Local file: content://com.android.providers.downloads.documents/document/xxx

Google Drive File: 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.

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.

If you have any better ideas, please give leave comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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