Java 如何在Android棒棒糖中使用WebView上传文件?
我是Android新手,我开发了一个webView应用程序,我有一个文件选择器按钮,但它在棒棒糖中不起作用。我在MI5棉花糖中安装了apk,没有任何响应 这是代码,请帮助我在哪里需要包括webview上的文件上载启用选项 活动\u main.xml:Java 如何在Android棒棒糖中使用WebView上传文件?,java,android,webview,Java,Android,Webview,我是Android新手,我开发了一个webView应用程序,我有一个文件选择器按钮,但它在棒棒糖中不起作用。我在MI5棉花糖中安装了apk,没有任何响应 这是代码,请帮助我在哪里需要包括webview上的文件上载启用选项 活动\u main.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context="com.example.tamil.stagingsite.MainActivity">
<ProgressBar
android:id="@+id/progressBar"
style="@android:style/Widget.Material.Light.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:animationCache="true"
android:background="@android:color/white">
</WebView>
</RelativeLayout>
在setwebchormeclinet中使用此代码。像这样
webView.setWebChromeClient(new WebChromeClient() {
// openFileChooser for Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){
// Update message
mUploadMessage = uploadMsg;
try{
// Create AndroidExampleFolder at sdcard
File imageStorageDir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
, "AndroidExampleFolder");
if (!imageStorageDir.exists()) {
// Create AndroidExampleFolder at sdcard
imageStorageDir.mkdirs();
}
// Create camera captured image file path and name
File file = new File(
imageStorageDir + File.separator + "IMG_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg");
mCapturedImageURI = Uri.fromFile(file);
// Camera capture image intent
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
// Create file chooser intent
Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
// Set camera intent to file chooser
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
, new Parcelable[] { captureIntent });
// On select image call onActivityResult method of activity
startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
catch(Exception e){
Toast.makeText(getBaseContext(), "Exception:"+e,
Toast.LENGTH_LONG).show();
}
}
// openFileChooser for Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsg){
openFileChooser(uploadMsg, "");
}
//openFileChooser for other Android versions
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType,
String capture) {
openFileChooser(uploadMsg, acceptType);
}
webView.setWebChromeClient(新WebChromeClient()){
//适用于Android 3.0的openFileChooser+
public void openFileChooser(ValueCallback uploadMsg,String acceptType){
//更新消息
mUploadMessage=上传消息;
试试{
//在SD卡上创建AndroidExample文件夹
File imageStorageDir=新文件(
Environment.getExternalStoragePublicDirectory(
环境。目录(图片)
,“AndroidExampleFolder”);
如果(!imageStorageDir.exists()){
//在SD卡上创建AndroidExample文件夹
imageStorageDir.mkdirs();
}
//创建相机捕获的图像文件路径和名称
文件=新文件(
imageStorageDir+File.separator+“IMG\ux”
+String.valueOf(System.currentTimeMillis())
+“.jpg”);
mCapturedImageURI=Uri.fromFile(文件);
//摄像机捕捉图像意图
最终意图捕获意图=新意图(
android.provider.MediaStore.ACTION\u IMAGE\u CAPTURE);
putExtra(MediaStore.EXTRA_输出,mCapturedImageURI);
意向i=新意向(意向.行动\u获取\u内容);
i、 addCategory(意图。类别可打开);
i、 setType(“image/*”);
//创建文件选择器意图
Intent chooserint=Intent.createChooser(i,“图像选择器”);
//将摄影机意图设置为文件选择器
选择content.putExtra(Intent.EXTRA\u初始意图
,新的包裹[]{captureIntent});
//在选择映像时调用ActivityResult活动方法
startActivityForResult(选择内容、文件选择器\u结果代码);
}
捕获(例外e){
Toast.makeText(getBaseContext(),“异常:”+e,
Toast.LENGTH_LONG).show();
}
}
//适用于Android<3.0的openFileChooser
public void openFileChooser(ValueCallback uploadMsg){
openFileChooser(uploadMsg,“”);
}
//其他Android版本的openFileChooser
public void openFileChooser(ValueCallback uploadMsg,
字符串接受类型,
字符串捕获){
openFileChooser(uploadMsg,acceptType);
}
这方面的参考链接是在setwebchormeclinet中使用此代码
webView.setWebChromeClient(new WebChromeClient() {
// openFileChooser for Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){
// Update message
mUploadMessage = uploadMsg;
try{
// Create AndroidExampleFolder at sdcard
File imageStorageDir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
, "AndroidExampleFolder");
if (!imageStorageDir.exists()) {
// Create AndroidExampleFolder at sdcard
imageStorageDir.mkdirs();
}
// Create camera captured image file path and name
File file = new File(
imageStorageDir + File.separator + "IMG_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg");
mCapturedImageURI = Uri.fromFile(file);
// Camera capture image intent
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
// Create file chooser intent
Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
// Set camera intent to file chooser
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
, new Parcelable[] { captureIntent });
// On select image call onActivityResult method of activity
startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
catch(Exception e){
Toast.makeText(getBaseContext(), "Exception:"+e,
Toast.LENGTH_LONG).show();
}
}
// openFileChooser for Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsg){
openFileChooser(uploadMsg, "");
}
//openFileChooser for other Android versions
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType,
String capture) {
openFileChooser(uploadMsg, acceptType);
}
webView.setWebChromeClient(新WebChromeClient()){
//适用于Android 3.0的openFileChooser+
public void openFileChooser(ValueCallback uploadMsg,String acceptType){
//更新消息
mUploadMessage=上传消息;
试试{
//在SD卡上创建AndroidExample文件夹
File imageStorageDir=新文件(
Environment.getExternalStoragePublicDirectory(
环境。目录(图片)
,“AndroidExampleFolder”);
如果(!imageStorageDir.exists()){
//在SD卡上创建AndroidExample文件夹
imageStorageDir.mkdirs();
}
//创建相机捕获的图像文件路径和名称
文件=新文件(
imageStorageDir+File.separator+“IMG\ux”
+String.valueOf(System.currentTimeMillis())
+“.jpg”);
mCapturedImageURI=Uri.fromFile(文件);
//摄像机捕捉图像意图
最终意图捕获意图=新意图(
android.provider.MediaStore.ACTION\u IMAGE\u CAPTURE);
putExtra(MediaStore.EXTRA_输出,mCapturedImageURI);
意向i=新意向(意向.行动\u获取\u内容);
i、 addCategory(意图。类别可打开);
i、 setType(“image/*”);
//创建文件选择器意图
Intent chooserint=Intent.createChooser(i,“图像选择器”);
//将摄影机意图设置为文件选择器
选择content.putExtra(Intent.EXTRA\u初始意图
,新的包裹[]{captureIntent});
//在选择映像时调用ActivityResult活动方法
startActivityForResult(选择内容、文件选择器\u结果代码);
}
捕获(例外e){
Toast.makeText(getBaseContext(),“异常:”+e,
Toast.LENGTH_LONG).show();
}
}
//适用于Android<3.0的openFileChooser
public void openFileChooser(ValueCallback uploadMsg){
openFileChooser(uploadMsg,“”);
}
//其他Android版本的openFileChooser
public void openFileChooser(ValueCallback uploadMsg,
字符串接受类型,
字符串捕获){
openFileChooser(uploadMsg,acceptType);
}
这方面的参考链接是