自定义图片选择器
/**
* 拍照或者从相册获取图片
* Created by PersonalFolder on 16/11/10.
*/
public class ImagePick implements View.OnClickListener {
private final int PICK_REQUEST = 0x1001;
private final int TAKE_REQUEST = 0x1002;
private static final String imgDir = Environment.getExternalStorageDirectory() + File.separator + "cloud";
public static String imgFile = imgDir + File.separator + "tmp.jpg";
private Activity activity;
private Dialog dialog;
public interface MyUri {
void getUri(Uri uri);
}
public interface MyDismiss {
void dismiss();
}
private MyDismiss md;
public ImagePick(Activity activity) {
this.activity = activity;
dialog = new Dialog(activity, R.style.DialogStyle);
//dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
View root = LayoutInflater.from(activity).inflate(R.layout.select_img, null);
// 设置点击监听
root.findViewById(R.id.take_photo).setOnClickListener(this);
root.findViewById(R.id.album_photo).setOnClickListener(this);
root.findViewById(R.id.cancel).setOnClickListener(this);
dialog.setContentView(root);
}
public void setCancel(MyDismiss md) {
this.md = md;
}
/**
* 显示Dialog
*/
public void show() {
//这样先md.dismiss可以保证dialog取消后可以再次打开
if (md != null) {
md.dismiss();
}
dialog.show();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.take_photo:
takePhoto();
break;
case R.id.album_photo:
getAlbum();
break;
case R.id.cancel:
if (md != null) {
md.dismiss();
}
break;
}
dialog.dismiss();
}
public void dismissImagePick(){
if (md != null) {
md.dismiss();
}
dialog.dismiss();
}
/**
* 拍照
*/
private void takePhoto() {
String sdState = Environment.getExternalStorageState();
// 如果SD卡可读写
if (sdState.equals(Environment.MEDIA_MOUNTED)) {
new File(imgDir).mkdirs();
File file = new File(imgFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
activity.startActivityForResult(intent, TAKE_REQUEST);
} else {
Toast.makeText(activity, "请确认已经插入SD卡", Toast.LENGTH_SHORT).show();
}
}
/**
* 从相册获取
*/
private void getAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setType("image/*");
activity.startActivityForResult(intent, PICK_REQUEST);
}
public void onActivityResult(int requestCode, int resultCode, Intent data, MyUri uri) {
if (resultCode != activity.RESULT_OK) {
return;
}
if (requestCode == PICK_REQUEST) {
if (data.getData() != null) {
uri.getUri(data.getData());
}
} else if (requestCode == TAKE_REQUEST) {
if (data != null) {
Uri myUri;
if (data.getData() != null) {
uri.getUri(data.getData());
return;
}
if (data.hasExtra("data")) {
Bitmap bitmap = data.getParcelableExtra("data");
try {
myUri = Uri.parse(MediaStore.Images.Media.insertImage(activity.getContentResolver(), bitmap, null, null));
uri.getUri(myUri);
} catch (Exception e) {
e.printStackTrace();
// 大神手机这行代码报异常:MediaStore.Images.Media.insertImage(activity.getContentResolver(), bitmap, null, null)
getUri(uri);
}
} else {
// Nexus 6 返回Intent{}得不到数据
getUri(uri);
}
} else {
getUri(uri);
}
}
}
private void getUri(MyUri uri) {
File file = new File(imgFile);
if (file != null && file.exists()) {
uri.getUri(Uri.fromFile(file));
}
}
/**
* 裁剪图片,并按照给定的长宽输出
*
* @param activity
* @param uri 拍照或者选择照片后获得的URI
* @param outputWidth 输出的宽度
* @param outputHeight 输出的高度
* @param requestCode 请求码
*/
public void cropImg(Activity activity, Uri uri, int outputWidth, int outputHeight, int requestCode) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", outputWidth);
intent.putExtra("aspectY", outputHeight);
intent.putExtra("outputX", outputWidth);
intent.putExtra("outputY", outputHeight);
intent.putExtra("outputFormat", "JPEG");
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
activity.startActivityForResult(intent, requestCode);
}
}
重写WebView辅助类WebChromeClient,返回数据给H5
private ValueCallback<Uri> mFilePathCallback;
private ValueCallback<Uri[]> mFilePathCallbackArray;
private ImagePick ip;
//...
public class MainWebChromeClient extends WebChromeClient {
// file upload callback (Android 2.2 (API level 8) -- Android 2.3 (API level 10)) (hidden method)
public void openFileChooser(ValueCallback<Uri> filePathCallback) {
handle(filePathCallback);
}
// file upload callback (Android 3.0 (API level 11) -- Android 4.0 (API level 15)) (hidden method)
public void openFileChooser(ValueCallback filePathCallback, String acceptType) {
handle(filePathCallback);
}
// file upload callback (Android 4.1 (API level 16) -- Android 4.3 (API level 18)) (hidden method)
public void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) {
handle(filePathCallback);
}
// for Lollipop
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, android.webkit.WebChromeClient.FileChooserParams fileChooserParams) {
// Double check that we don't have any existing callbacks
if (mFilePathCallbackArray != null) {
mFilePathCallbackArray.onReceiveValue(null);
}
mFilePathCallbackArray = filePathCallback;
showDialog();
return true;
}
/**
* 处理5.0以下系统回调
*
* @param filePathCallback
*/
private void handle(ValueCallback<Uri> filePathCallback) {
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
showDialog();
}
/**
* 显示照片选取Dialog
*/
public void showDialog() {
if (ip == null) {
ip = new ImagePick(MainActivity.this);
}
ip.setCancel(new ImagePick.MyDismiss() {
@Override
public void dismiss() {
handleCallback(null);
}
});
ip.show();
}
/**
* 拦截js弹窗
* @param view
* @param url
* @param message
* @param result
* @return
*/
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
if (!message.contains("404") && !message.contains("服务器异常") && !message.contains("网络无法连接")) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
result.confirm();
return true;
}
/**
* Webview加载页面进度
*/
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) { // 正常照片选取的时候调用
ip.onActivityResult(requestCode, resultCode, data, new ImagePick.MyUri() {
@Override
public void getUri(Uri uri) {
handleCallback(uri);
}
});
} else {
// 取消了照片选取的时候调用
handleCallback(null);
}
}
/**
* 处理WebView的回调
*
* @param uri
*/
private void handleCallback(Uri uri) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mFilePathCallbackArray != null) {
if (uri != null) {
mFilePathCallbackArray.onReceiveValue(new Uri[]{uri});
} else {
mFilePathCallbackArray.onReceiveValue(null);
}
mFilePathCallbackArray = null;
}
} else {
if (mFilePathCallback != null) {
if (uri != null) {
mFilePathCallback.onReceiveValue(uri);
} else {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = null;
}
}
}
通过上面两大步就可以完成WebView其调用ImagePicker来选择图片,然后通过将用户图片数据返回给网页。