webview上传图片
import java.io.File;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
private String url = "http://101.200.142.201";
private WebView my_web;
private ProgressDialog proDlg;
private Uri imageUri;
public ValueCallback<Uri> mUploadMessage;
public final static int REQ_CHOOSER = 1;
private final static int REQ_CAMERA = 2;
@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
my_web = (WebView) findViewById(R.id.wv);
// 支持脚本
my_web.getSettings().setJavaScriptEnabled(true);
//加载服务器的路径
my_web.loadUrl(url);
//创建对话框
proDlg = new ProgressDialog(MainActivity.this);
my_web.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// my_web.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
proDlg.setMessage("正在加载,请稍候...");
// 显示加载进度对话框
proDlg.show();
}
/**
* 页面加载完成时,回调此方法
*/
@Override
public void onPageFinished(WebView view, String url) {
// 隐藏加载进度对话框
proDlg.dismiss();
}
});
my_web.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
proDlg.setMessage("loading" + newProgress + "%......");
}
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooser(uploadMsg, "");
}
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg,
String acceptType) {
openFileChooser(uploadMsg, acceptType);
}
// For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
mUploadMessage = uploadMsg;
selectImage();
}
});
}
protected final void selectImage() {
AlertDialog.Builder builder = new Builder(MainActivity.this);
// builder.setTitle("插入照片");
final String[] items = { "照相机", "相册" };
builder.setItems(items, new DialogInterface.OnClickListener() {
@SuppressLint("SdCardPath")
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), items[which],
Toast.LENGTH_SHORT).show();
Intent intent = null;
switch (which) {
// 调用照相机
case 0:
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 必须确保文件夹路径存在,否则拍照后无法完成回调
File vFile = new File(Environment
.getExternalStorageDirectory().getPath()
+ "/DCIM/Camera/"
+ (System.currentTimeMillis() + ".jpg"));
if (!vFile.exists()) {
File vDirPath = vFile.getParentFile();
vDirPath.mkdirs();
} else {
if (vFile.exists()) {
vFile.delete();
}
}
imageUri = Uri.fromFile(vFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
MainActivity.this
.startActivityForResult(intent, REQ_CAMERA);
break;
case 1:
intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
MainActivity.this.startActivityForResult(
Intent.createChooser(intent, "选择图片"), REQ_CHOOSER);
break;
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
if(mUploadMessage!=null){
mUploadMessage.onReceiveValue(null);
selectImage();
}
}
});
builder.create().show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CHOOSER:
if (null == mUploadMessage)
return;
Uri result = data == null || resultCode != RESULT_OK ? null : data
.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
break;
case REQ_CAMERA:
if (resultCode == Activity.RESULT_OK) {
mUploadMessage.onReceiveValue(imageUri);
mUploadMessage = null;
}
break;
}
}
// 此按键监听的是返回键,能够返回到上一个网页(通过网页的hostlistery)
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && my_web.canGoBack()) {
my_web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}