package com.example.definedialog.view;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.example.definedialog.R;
public class WebviewChoosefile extends Activity {
private WebView webview;
private xWebChromeClient xwebchromeclient;
private ValueCallback<Uri> mUploadMessage = null;
private ValueCallback<Uri[]> mFilePathCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_layout);
webview = (WebView) findViewById(R.id.webview);
webview.setInitialScale(25);// 为25%,最小缩放等级
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);// 支持js
settings.setUseWideViewPort(true);// 将图片调整到适合webview的大小
// settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);//支持内容重新布局
settings.setLoadWithOverviewMode(true);
settings.setDefaultTextEncodingName("utf-8");
// 缓存
// settings.setCacheMode(WebSettings.LOAD_DEFAULT);
webview.loadUrl("file:///android_asset/index2.html");
xwebchromeclient = new xWebChromeClient();
webview.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return true;// 长按不出现复制粘贴
}
});
webview.setWebViewClient(new MyWebViewClient());
webview.setWebChromeClient(xwebchromeclient);
}
public class xWebChromeClient extends WebChromeClient {
// For Android 4.1_4.4
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
mUploadMessage = uploadMsg;
openCamera();
// 选择返回 ,只接受uri返回
}
// // Android 5.0
public boolean onShowFileChooser(WebView webView,
ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
mFilePathCallback = filePathCallback;
openCamera();
return true;
}
}
private void openCamera() {
Intent i = new Intent(Intent.ACTION_PICK);// 打开相册
i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(i, 1);
}
Uri[] results = null;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 选择本地返回
if (requestCode == 1) {
Uri uri = data.getData();
System.out.println(uri.toString());
// mUploadMessage.onReceiveValue(uri);//Adroid 5.0以下返回
// Android 5.0及以上选择文件返回
results = new Uri[] { uri };
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
}
super.onActivityResult(requestCode, resultCode, data);
}
private class MyWebViewClient extends android.webkit.WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 在webview 内部打开新的url
view.loadUrl(url);
return true;
}
}
}
WebView中 支持input type=file
最新推荐文章于 2022-07-23 16:00:30 发布