Java Android WebView未打开输入类型文件上的文件选择器或照相机
我的androidwebview应用程序在filechooser上没有响应 以下是我的代码: MainActivity.javaJava Android WebView未打开输入类型文件上的文件选择器或照相机,java,android,webview,Java,Android,Webview,我的androidwebview应用程序在filechooser上没有响应 以下是我的代码: MainActivity.java package in.marksys.www.attendancemanagement; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent;
package in.marksys.www.attendancemanagement;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.webkit.ConsoleMessage;
import android.webkit.ValueCallback;
import android.webkit.WebBackForwardList;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends Activity {
private WebView webView;
private Toast toast;
private long lastBackPressTime = 0;
//File choser parameters
private static final int FILECHOOSER_RESULTCODE = 2888;
final Activity activity = this;
public Uri imageUri;
private ValueCallback<Uri> mUploadMessage;
//Camera parameters
private Uri mCapturedImageURI = null;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get webview
webView = (WebView) findViewById(R.id.webView);
// Define url that will open in webview
String webViewUrl = "http://marksystest.in/attendance/applogin.php";
// Javascript inabled on webview
webView.getSettings().setJavaScriptEnabled(true);
// Other webview options
webView.getSettings().setLoadWithOverviewMode(true);
//webView.getSettings().setUseWideViewPort(true);
//Other webview settings
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setSupportZoom(true);
//Load url in webview
webView.loadUrl(webViewUrl);
// Define Webview manage classes
startWebView();
}
private void startWebView() {
// Create new webview Client to show progress dialog
// Called When opening a url or click on link
// You can create external class extends with WebViewClient
// Taking WebViewClient as inner class
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
//If you will not use this method url links are open in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Check if Url contains ExternalLinks string in url
// then open url in new browser
// else all webview links will open in webview browser
if(url.contains("google")){
// Could be cleverer and use a regex
//Open links in new browser
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
// Here we can open new activity
return true;
} else {
// Stay within this webview and load url
view.loadUrl(url);
return true;
}
}
//Show loader on url load
public void onLoadResource (WebView view, String url) {
// if url contains string androidexample
// Then show progress Dialog
if (progressDialog == null && url.contains("androidexample")
) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
// Called when all page resources loaded
public void onPageFinished(WebView view, String url) {
try{
// Close progressDialog
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
});
// You can create external class extends with WebChromeClient
// Taking WebViewClient as inner class
// we will define openFileChooser for select file from camera or sdcard
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);
}
// The webPage has 2 filechoosers and will send a
// console message informing what action to perform,
// taking a photo or updating the file
public boolean onConsoleMessage(ConsoleMessage cm) {
onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId());
return true;
}
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
//Log.d("androidruntime", "Show console messages, Used for debugging: " + message);
}
}); // End setWebChromeClient
}
// Return here when file selected from camera or from SDcard
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE)
{
if (null == this.mUploadMessage) {
return;
}
Uri result=null;
try{
if (resultCode != RESULT_OK) {
result = null;
} else {
// retrieve from the private variable if the intent is null
result = intent == null ? mCapturedImageURI : intent.getData();
}
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "activity :"+e,
Toast.LENGTH_LONG).show();
}
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
@Override
// Detect when the back button is pressed
public void onBackPressed() {
if(webView.canGoBack())
{
WebBackForwardList mWebBackForwardList = webView.copyBackForwardList();
String historyUrl = mWebBackForwardList.getItemAtIndex(mWebBackForwardList.getCurrentIndex()-1).getUrl();
String loginurl = "http://marksystest.in/attendance/applogin.php";
if(historyUrl==loginurl)
{
super.onBackPressed();
// toast = Toast.makeText(this, historyUrl, Toast.LENGTH_SHORT);
//toast.show();
}
else {
webView.goBack();
}
}
else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
package in.marksys.www.attendancemanagement;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.net.Uri;
导入android.os.Bundle;
导入android.os.Environment;
导入android.os.Parcelable;
导入android.provider.MediaStore;
导入android.webkit.ConsoleMessage;
导入android.webkit.ValueCallback;
导入android.webkit.WebBackForwardList;
导入android.webkit.WebChromeClient;
导入android.webkit.WebSettings;
导入android.webkit.WebView;
导入android.webkit.WebViewClient;
导入android.widget.Toast;
导入java.io.File;
公共类MainActivity扩展了活动{
私有网络视图;
私人吐司;
私人长时间lastBackPressTime=0;
//文件选择器参数
私有静态final int FILECHOOSER_RESULTCODE=2888;
最终活动=此;
公共Uri-imageUri;
private ValueCallback mUploadMessage;
//摄像机参数
私有Uri mCapturedImageURI=null;
@SuppressLint(“SetJavaScriptEnabled”)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取网络视图
webView=(webView)findviewbyd(R.id.webView);
//定义将在webview中打开的url
字符串webViewUrl=”http://marksystest.in/attendance/applogin.php";
//在webview上不使用Javascript
webView.getSettings().setJavaScriptEnabled(true);
//其他网络视图选项
webView.getSettings().setLoadWithOverview模式(true);
//webView.getSettings().setUseWideViewPort(true);
//其他网络视图设置
webView.setScrollBarStyle(webView.SCROLLBARS\u外部\u覆盖);
webView.SetScrollBarFadinEnabled(假);
webView.getSettings().setBuilTinZoomControl(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setSupportZoom(true);
//在webview中加载url
loadUrl(webViewUrl);
//定义Webview管理类
startWebView();
}
私有void startWebView(){
//创建新的webview客户端以显示进度对话框
//打开url或单击链接时调用
//您可以使用WebViewClient创建外部类扩展
//将WebViewClient作为内部类
setWebViewClient(新的WebViewClient(){
进行对话进行对话;
//如果不使用此方法,url链接将在新浏览器中打开,而不是在webview中打开
公共布尔值shouldOverrideUrlLoading(WebView视图,字符串url){
//检查Url中是否包含ExternalLinks字符串
//然后在新浏览器中打开url
//否则,所有webview链接都将在webview浏览器中打开
if(url.contains(“谷歌”)){
//可以更聪明,使用正则表达式
//在新浏览器中打开链接
view.getContext().startActivity(
新的意图(Intent.ACTION_视图,Uri.parse(url));
//在这里我们可以开始新的活动
返回true;
}否则{
//保持在此webview内并加载url
view.loadUrl(url);
返回true;
}
}
//在url加载时显示加载程序
public void onLoadResource(WebView视图,字符串url){
//如果url包含字符串androidexample
//然后显示进度对话框
if(progressDialog==null&&url.contains(“androidexample”)
) {
//在标准情况下,您的活动是
progressDialog=新建progressDialog(MainActivity.this);
progressDialog.setMessage(“加载…”);
progressDialog.show();
}
}
//在加载所有页面资源时调用
公共void onPageFinished(WebView视图,字符串url){
试一试{
//关闭进程对话框
if(progressDialog.isShowing()){
progressDialog.disclose();
progressDialog=null;
}
}捕获(异常){
异常。printStackTrace();
}
}
});
//您可以使用WebChromeClient创建外部类扩展
//将WebViewClient作为内部类
//我们将定义openFileChooser,用于从相机或SD卡选择文件
setWebView.WebChromeClient(新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+
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.marksys.www.attendancemanagement">
<uses-sdk
android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-feature android:name="android.hardware.camera"></uses-feature>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
webView = (WebView) findViewById(R.id.web_view_sso);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
});
webView.loadUrl(AppConstant.SSO_LOGIN_URL);
}