【发布时间】:2016-07-26 11:52:54
【问题描述】:
当我在使用 web 视图的地方打开我的应用程序时,我点击了文件选择器,然后什么也没发生。当我尝试在浏览器中的网站上时,它可以工作。
为什么文件选择器在 Android Webview 中不起作用?
【问题讨论】:
标签: android webview filechooser
当我在使用 web 视图的地方打开我的应用程序时,我点击了文件选择器,然后什么也没发生。当我尝试在浏览器中的网站上时,它可以工作。
为什么文件选择器在 Android Webview 中不起作用?
【问题讨论】:
标签: android webview filechooser
您必须在代码中实现 javascript 接口。基本上您必须与 Javascript 和您的 Activity 进行通信。 考虑这个示例并根据您的需要进行更改。
https://www.opengeeks.me/2015/08/filechooser-and-android-webview/
【讨论】:
使用此代码在 webview 打开时从设备获取文件:
web.setWebChromeClient(new WebChromeClient()
{
//The undocumented magic method override
//Eclipse will swear at you if you try to put @Override here
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MyWb.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);
}
// For Android 3.0+
public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
MyWb.this.startActivityForResult(
Intent.createChooser(i, "File Browser"),
FILECHOOSER_RESULTCODE);
}
//For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MyWb.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MyWb.FILECHOOSER_RESULTCODE );
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage) return;
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
【讨论】: