java - 谷歌驱动器文件上传在Android Webview中不起作用
问题描述
我正在将文件上传到 WebView。本地文件运行良好。但是来自谷歌驱动器的文件不起作用。本地文件的URI信息不为空。(不为空)
本地文件:content://com.android.providers.downloads.documents/document/xxx
谷歌云端硬盘文件: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"));
...
我不知道发生这种情况的原因。也许我必须检查网页?
请帮我... :(
解决方案
我在本地保存了 google 驱动器文件并将该本地 uri 发送到 webview。
Intent intent = new Intent();
intent.setData(Uri.fromFile(savedLocalFile(uri));
WebChromeClient.FileChooserParams.parseResult(resultCode, intent);
该savedLocalFile()
函数返回一个File
存储在本地(或外部)存储中。
如果您有更好的想法,请留下评论。
推荐阅读
- python - 如何创建无密码的 FTP 连接?
- ios - Swift 5 CAKeyframeAnimation - 应用程序移动到免费后恢复图层动画
- java - 有没有办法从测试容器中传递 GenericContainer 中的 docker 标志
- bash - 带有 for 的多行 ssh 命令
- cakephp - CakePHP TinyAuth 适配器实现
- shell - 合并多个 csv 文件,其中包含混合的标题和没有标题
- sql - 如何在不同的表中添加基于 categoryid 的外键?
- javascript - angularjs $resource: [资源中的动作名称] 不是函数
- python-3.x - 数据框:将基于行的交易数据转换为每个日期的聚合
- c++ - 将结构向量保存到文件中并从 C++ 文件中读取结构向量