gpt4 book ai didi

android - 使用 html 表单在 Android 2.2.1 webview 中上传文件

转载 作者:行者123 更新时间:2023-11-28 03:43:42 26 4
gpt4 key购买 nike

我正在使用 webview 开发 Android 2.2.1 应用程序。我似乎找不到文件上传的有效解决方案。这是一个标准的文件上传表单。 Webview 从不弹出浏览器框。在网络浏览器本身下,一切正常。在 webview 中,没有浏览弹出窗口。如果您知道解决方案,请准确告诉我将添加的代码放在 android 代码部分的什么位置。

下面的文件上传表单:

<form name="form" id="fileupload" enctype="multipart/form-data" action="upload.asp" method="post">
<input id="blob" name="blob" class="simpleinput" type="file" />
</form>

Android 代码如下:

package com.pic21.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class pic21 extends Activity {
/** Called when the activity is first created. */

WebView mWebView;

private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.m.blah.com");
mWebView.setWebViewClient(new HelloWebViewClient());
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

最佳答案

(由 OP 在问题编辑中回答。参见 Question with no answers, but issue solved in the comments (or extended in chat))

OP 写道:

I looked around for a bit more and found code that works using the webChromeClient. Working code below:

package com.pic21.app;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressWarnings("unused")
public class pic extends Activity {

private WebView mWebView;

private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE=1;

@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;
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mWebView = new WebView(this);
mWebView.setWebViewClient(new WebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.m.blah.com");
mWebView.setWebChromeClient(new WebChromeClient()

{
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg) {

mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
pic21Activity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);

}
});

setContentView(mWebView);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

}

The code for the AndroidManifest.xml file should contain the internet permission

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pic21.app"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET" />

<uses-sdk android:minSdkVersion="7" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".pic21Activity"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|keyboard" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

关于android - 使用 html 表单在 Android 2.2.1 webview 中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8469597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com