package com.partynetwork.myview.myimageview;
import java.io.File;
import java.util.Date;
import com.partynetwork.dataprovider.util.BitmapUtil;
import com.partynetwork.myview.mytoast.MenuBottomPop;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class ImagePicker {
private Activity mActivity;
public static final int SELECT_PICTURE = 100;
public static final int SELECT_CAMER = 101;
public static final int SELECT_CAMER_CLIPPING = 102;
// 是否需要保存
private boolean needSave;
/** 本次生成的路径 */
private String imagePath;
// 弹出框
private MenuBottomPop menuBottonPop;
/**
*
* @param activity
* @param needClipping
* 是否需要缩放
*/
public ImagePicker(Activity activity) {
this(activity, false);
}
/**
*
* @param activity
* @param needClipping
* @param needSave
*/
public ImagePicker(Activity activity, boolean needSave) {
this.mActivity = activity;
init();
}
/**
* 初始化
*/
private void init() {
}
String[] str = new String[] { "相册" };//相机
private OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
menuBottonPop.dismiss();
if (v.getTag().equals(str[0])) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
mActivity.startActivityForResult(intent, SELECT_PICTURE);
} else if (v.getTag().equals(str[1])) {
Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imagePath = BitmapUtil.getImagePath();
Uri uri = Uri.fromFile(new File(imagePath));
intent2.putExtra(MediaStore.EXTRA_OUTPUT, uri);
mActivity.startActivityForResult(intent2, SELECT_CAMER);
}
}
};
/**
* 显示选择弹窗
*/
public void show(View view) {
menuBottonPop = new MenuBottomPop(mActivity, str, clickListener);
menuBottonPop.showAtLocation(view);
}
/**
* 处理图片的返回数据
*/
public String onActivityResult(int requestCode, int resultCode, Intent data) {
String filePath = "";
Object object = null;
// 选择照片的返回结果
if (data == null) {
filePath = imagePath;
// 根据地址,实例化对象
return filePath;
}
if (resultCode == Activity.RESULT_OK) {
Bitmap bitmap = null;
switch (requestCode) {
case SELECT_PICTURE:
// 相册
Uri uri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = mActivity.managedQuery(uri, proj, null, null,
null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
filePath = cursor.getString(column_index);
break;
case SELECT_CAMER:
// 相机
if (data.getData() != null) {
// 根据返回的URI获取对应的SQLite信息
Cursor cursor2 = mActivity.getContentResolver().query(
data.getData(), null, null, null, null);
if (cursor2.moveToFirst()) {
filePath = cursor2.getString(cursor2
.getColumnIndex(Media.DATA));// 获取绝对路径
}
cursor2.close();
} else {// 三星 小米(小米手机不会自动存储DCIM... 这点让哥又爱又恨...)
object = (Bitmap) (data.getExtras() == null ? null : data
.getExtras().get("data"));
bitmap = object == null ? null : (Bitmap) object;
if (bitmap != null) {
try {
filePath = BitmapUtil.saveImg(bitmap, DateFormat
.format("yyyyMMddhhmmss", new Date())
.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
filePath = !filePath.equals("") ? filePath : getRealFilePath();
break;
default:
break;
}
// 最后的图片操作
if (bitmap != null && needSave) {
try {
filePath = BitmapUtil.saveImg(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
Toast.makeText(mActivity, "请重新选择图片", Toast.LENGTH_SHORT).show();
}
return filePath;
}
/**
* 获取系统默认存储真实文件路径 两个路径 一个是最后一张所拍图片路径
* (由于设置了存储路径所以DCIM中会存储两张一模一样的图片,所以在此获取两张图片路径以便于缩放处理后全部删除)
*
* @param filePath
* @return
*/
protected String getRealFilePath() {
String filePath = "";
// MediaStore.Images.Media.EXTERNAL_CONTENT_URI
// content://media/external/images/media
Cursor cursor = mActivity.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,
Media.DATE_MODIFIED + " desc");
if (cursor.moveToNext()) {
/**
* _data:文件的绝对路径 Media.DATA='_data'
*/
filePath = cursor.getString(cursor.getColumnIndex(Media.DATA));
}
return filePath;
}
}