AndroidWebview应用程序中启用文件上传功能。
创始人
2024-10-12 03:30:54
0

要在Android Webview应用程序中启用文件上传功能,可以按照以下步骤进行操作:

1.在AndroidManifest.xml文件中添加以下代码以获取WRITE_EXTERNAL_STORAGE权限:

2.在Webview设置中启用JavaScript:

webView.getSettings().setJavaScriptEnabled(true);

3.为Webview设置WebChromeClient:

webView.setWebChromeClient(new WebChromeClient() { //文件选择器数据 public void openFileChooser(ValueCallback uploadMsg) {

    //创建包含传递文件URI的意图
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");

    //启动文件选择器
    startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILECHOOSER_RESULTCODE);
}

// For Android  >= 3.0
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
    openFileChooser(uploadMsg, acceptType, "");
}

// For Android  >= 4.1
public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
    mUploadMessage = uploadMsg;
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILECHOOSER_RESULTCODE);
}

// For Android > 5.0
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
    mFilePathCallback = filePathCallback;
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILECHOOSER_RESULTCODE);
    return true;
}

});

4.在onActivityResult方法中处理文件选择器意图:

@Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == FILECHOOSER_RESULTCODE) { if (resultCode == Activity.RESULT_OK && intent

相关内容

热门资讯

透视安卓版"wepo... 1、透视安卓版"wepokerplus万能挂"详细辅助透明教程(俱乐部辅助器透视)。2、wepoke...
透视黑科技"hhpo... 透视黑科技"hhpoker是内部控制吗"详细辅助解说技巧(透视脚本下载攻略);hhpoker是内部控...
透视黑科技"wepo... 透视黑科技"wepoker轻量版透视方法"详细辅助详细教程(透视脚本免费app攻略)准备好在wepo...
透视了解"aapok... 1、透视了解"aapoker俱乐部靠谱吗"详细辅助透明挂教程(辅助脚本攻略)2、进入游戏-大厅左侧-...
透视了解"hhpok... 透视了解"hhpoker透视方法"详细辅助揭秘攻略(可以挂底牌技巧)1、点击下载安装,微扑克wpk插...
透视app"约局吧开... 透视app"约局吧开挂神器是真的吗"详细辅助第三方教程(有透视方法技巧);最新版2024是一款经典耐...
透视了解"wepok... 透视了解"wepoker私人局俱乐部怎么进"详细辅助爆料教程(有挂神器);实战中需综合运用上述技巧,...
透视软件"wepok... 透视软件"wepoker私人局外卦"详细辅助AI教程(正确养号方法技巧)是一款可以让一直输的玩家,快...
透视脚本"we po... 透视脚本"we poker辅助器v3.3"详细辅助安装教程(辅助器激活码透视)准备好在we poke...
透视插件"德州透视脚... 透视插件"德州透视脚本"详细辅助存在挂教程(透视挂底牌攻略);科技安装教程;136704302。相信...