。。
public class ChoiceImage {
public static final int CAMERA = 1000;
public static final int PICTURE = 2000;
public static final int CROP_RESULT = 30000;//结果
private File f;
private Activity activity;
/**
*
*/
public ChoiceImage(Activity activity) {
this.activity=activity;
}
public void goCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir = new File(Environment.getExternalStorageDirectory()+ File.separator + ".temp");
if (!dir.exists())
dir.mkdirs();
f = new File(dir, String.valueOf(System.currentTimeMillis()).concat(".jpg"));
Uri u = Uri.fromFile(f);
intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
intent.putExtra("test", 1111);
activity.startActivityForResult(intent, CAMERA);
}
public File getF() {
return f;
}
public void startCrop(Uri uri,Activity activity) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("return-data", true);
intent.putExtra("noFaceDetection", true);
activity.startActivityForResult(intent, CROP_RESULT);
}
/**
*
*/
public void goGallery() {
Intent picture = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
activity.startActivityForResult(picture, PICTURE);
}
/**
* 不截图需要转换Uri的到String路径
* Uri 转file
* @param uri
* @return
*/
public String getAbsoluteImagePath(Uri uri,Context context)
{
// can post image
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = ((Activity)context).managedQuery( uri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">//创建实列</span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span style="white-space:pre"> </span>if (choiceImage == null)
<span style="white-space:pre"> </span>choiceImage = new ChoiceImage(EditHeadActivity.this);
</span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">//拍照或图库的点击事件</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);"><span style="white-space:pre"> </span>btn_takephoto.setOnClickListener(new View.OnClickListener() {
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onClick(View v) {
<span style="white-space:pre"> </span>String status = Environment.getExternalStorageState();
<span style="white-space:pre"> </span>if(status.equals(Environment.MEDIA_MOUNTED))//判断是否有SD卡
<span style="white-space:pre"> </span>{choiceImage.goCamera();
<span style="white-space:pre"> </span>takedialog.dismiss();}
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>Toast.makeText(context, "没sdcard",1000).show();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});
<span style="white-space:pre"> </span>btn_photo.setOnClickListener(new View.OnClickListener() {
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onClick(View v) {
<span style="white-space:pre"> </span>choiceImage.goGallery();
<span style="white-space:pre"> </span>takedialog.dismiss();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});
</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">//第一种裁剪过后获取的图片路径</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);"><span style="white-space:pre"> </span> public void onActivityResult(int requestCode, int resultCode, Intent data) {
<span style="white-space:pre"> </span>if (resultCode != Activity.RESULT_OK)
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>File filepath = null;
<span style="white-space:pre"> </span>switch (requestCode){
<span style="white-space:pre"> </span>case ChoiceImage.CAMERA:// 从照相机拍得照片
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span> Uri imageuri = Uri.fromFile(choiceImage.getF());
<span style="white-space:pre"> </span> choiceImage.startCrop(imageuri , EditHeadActivity.this);
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>case ChoiceImage.PICTURE:// 从图库中获得照片
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>Uri imageuri = data.getData();
<span style="white-space:pre"> </span>//filepath = new File(getAbsoluteImagePath(imageuri));
<span style="white-space:pre"> </span>choiceImage.startCrop(imageuri, EditHeadActivity.this);
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>case ChoiceImage.CROP_RESULT:
<span style="white-space:pre"> </span>byte[] imgdata = null;
<span style="white-space:pre"> </span>Bundle extras = data.getExtras();
<span style="white-space:pre"> </span>Bitmap bitmap = (Bitmap)extras.get("data");
<span style="white-space:pre"> </span>filepath=ImageUtil.saveMyBitmap(bitmap);
<span style="white-space:pre"> </span>takedialog.dismiss();
<span style="white-space:pre"> </span>upload_img(filepath.getAbsolutePath());</span></span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 11.8181819915771px; background-color: rgb(255, 255, 255);">//得到图片路径</span><span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">//第二种从图库或拍照时未经过处理的原图路径</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);"><span style="white-space:pre"> </span>public void onActivityResult(int requestCode, int resultCode, Intent data) {
<span style="white-space:pre"> </span>if (resultCode != Activity.RESULT_OK)
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>switch (requestCode) {
<span style="white-space:pre"> </span>case ChoiceImage.CAMERA:// 从照相机拍得照片
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>String path = choiceImage.getF().getAbsolutePath();
<span style="white-space:pre"> </span>File file=choiceImage.getF();
<span style="white-space:pre"> </span>upload_img(file.getAbsolutePath());</span></span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 11.8181819915771px; background-color: rgb(255, 255, 255);">//得到图片路径</span><span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>case ChoiceImage.PICTURE:// 从图库中获得照片
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>Uri imageuri = data.getData();
<span style="white-space:pre"> </span>File file = new File(choiceImage.getAbsoluteImagePath(imageuri,context));
<span style="white-space:pre"> </span>upload_img(file.getAbsolutePath());//得到图片路径
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
</span></span>