解决方法在Android 4.4使用反射文件上传 [英] Workaround for file uploads on Android 4.4 using reflection

查看:737
本文介绍了解决方法在Android 4.4使用反射文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Android 4.1到Android 4.3的,你可以继承 WebChromeClient Android和实施以下(隐藏,因此没有 @覆盖)方法:

From Android 4.1 to Android 4.3, you could subclass WebChromeClient on Android and implement the following (hidden, thus no @Override) method:

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { ... }

您会在该方法打开一个文件选择器,当你的活动接收的onActivityResult(...),只需调用可用的 ValueCallback&LT单一的方法;开放的&GT; 与文件URI实例。这就是它。

You would open a file picker in that method, and when your Activity receives the result in onActivityResult(...), just call the single method that is available on the ValueCallback<Uri> instance with the file URI. That's it.

不幸的是,从在Android 4.4系统的API中删除此方法并没有被称为了

Unfortunately, this method was removed from the API in Android 4.4 and is not being called anymore.

时出现的可以解决这个问题,例如任何的方式与反思?

Is there any way you can work around this, e.g. with reflection?

我的意思是,你可以在设置的onclick =...监听器&LT;输入类型=文件&GT; 在HTML元素,它仍然会被调用。

I mean, you could set an onclick="..." listener on the <input type="file"> element in HTML, which would still be called.

也许通过 addJavascriptInterface(...)拦截在Java中(即的onclick =...回调后),你可以打开一个文件选择器如常。但问题是,有没有 ValueCallback&LT;乌里&GT; 实例可用,你可以把你的结果(即被选中的文件)

After intercepting the onclick="..." callback in Java (maybe via addJavascriptInterface(...)) you could open a file picker as usual. But the problem is that there's no ValueCallback<Uri> instance available that you could send your result (the file that was picked) to.

设置值=...&LT;输入类型=文件&GT; 在HTML中手动是不可能的,或者,因为它是只读的。但的WebView 的实施本身当然可以写值,很明显。所以,你可以使用反射存在设定值或另寻解决办法?

Setting the value="..." of the <input type="file"> in HTML manually is not possible, either, because it's read-only. But the WebView implementation itself can certainly write the value, obviously. So could you use reflection to set the value there or find another workaround?

我在寻找一个更清洁,更舒适的解决方案比检测&LT;输入类型=文件&GT; 点击(例如,通过它的的onclick =...监听器),并从Java人工手动操作文件的选择和文件上传。启用文件上传的在web视图的,因为他们要工作,会是更清洁,当然可以。

I'm searching for a cleaner and more comfortable solution than detecting the <input type="file"> click (e.g. via its onclick="..." listener) and doing the file selection and file upload manually from Java. Enabling file uploads in the WebView, as they should work, would be cleaner, certainly.

推荐答案

如果你正在寻找一个干净的解决方案您这样做不使用棘手的Javascript codeS和探测器。我建议你​​使用的是网页视图的选择。

If you're looking for a clean solution to do this this without using tricky Javascript codes and detectors. I'd recommend using a Webview alternative.

人行横道结果
我建议这一点。它会工作类似官方网页视图。它是开源的,并背后 ChromeView 相同的人进行。他们有一个伟大的设置指南: https://crosswalk-project.org/documentation/embedding_crosswalk。 HTML

Crosswalk
I'd recommend this. It'll work similar to the official webview. It's open source and made by the same people behind ChromeView. They've got a great setup guide: https://crosswalk-project.org/documentation/embedding_crosswalk.html

检查出来: https://crosswalk-project.org/

铬网页视图
结果又被ChromeView启发。他们声称已经集成在同一个API作为易于移植的官方网页流量。但他们表示web视图仍然可能是一个有点不稳定。

Chromium Webview
Also inspired by ChromeView. They claim to have integrated the same API's as the official webview for easy migration. But they indicate that the Webview still might be a bit unstable.

检查出来: https://github.com/mogoweb/chromium_webview

GeckoView
结果由Mozilla而成。下面是一个使用它的示范项目。 Mozilla的警告说:这不是一个下降的WebView更换,很不稳定,但它有文件上传

GeckoView
Made by Mozilla. Here's a demo project that uses it. Mozilla warns: "This is not a drop webview replacement and very unstable", but it has file upload

检查出来: https://wiki.mozilla.org/Mobile/GeckoView

如果你在使用Javascript开(也许有用替他人)相关搜索:结果

If you're open on using Javascript (perhaps usefull for others) try:

奇巧网页视图结果
它使用JavaScript来启用奇巧的WebView文件上传做到这一点。该演示,让您上传图片,但你应该能够编辑它以满足您的需求。

Kitkat Webview
It uses javascript to enable file upload in KitKat webview do this. The demo allows you to upload an image, but you should be able to edit it to fit your needs.

检查出来: https://github.com/henrychuangtw/Kitkat-WebView

这篇关于解决方法在Android 4.4使用反射文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆