通过照相机拍照或者图库选择图片上传给服务器作为头像

时间:2022-09-18 23:07:18

通过照相机拍照或者图库选择图片上传给服务器作为头像

一个自定义的dialog

public class SettingAvatarDialog extends Dialog implements View.OnClickListener{

public static final int CAMERA = 33;
public static final int PICTRUE_LIBS = 22;
public static final int CUTTING = 11;
public static final String IMAGE_UNSPECIFIED = "image/*";

private View view;
private TextView tv_camera;
private TextView tv_picture_libs;
private TextView tv_cancel;
private Activity mActivity;

/**
* @param context
* @param cancelable
* @param cancelListener
*/
public SettingAvatarDialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}

/**
* @param context
* @param theme
*/
public SettingAvatarDialog(Context context, int theme) {
super(context, theme);
}

/**
* @param context
*/
public SettingAvatarDialog(Context context) {
super(context);
this.mActivity = (Activity) context;
requestWindowFeature(Window.FEATURE_NO_TITLE);
view = View.inflate(context,R.layout.activity_camera,null);
this.setContentView(view);
initView();
initOnclickListener();
}

/**
* 初始化位置,让其位于屏幕的下方
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setGravity(Gravity.BOTTOM);

Display display = getWindow().getWindowManager().getDefaultDisplay();
int width = display.getWidth();

WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.width = width;
getWindow().setAttributes(attributes);
this.setCancelable(false);

}

/**
* 初始化点击事件
*/
private void initOnclickListener() {
tv_camera.setOnClickListener(this);
tv_picture_libs.setOnClickListener(this);
tv_cancel.setOnClickListener(this);
}

/**
* 初始化子控件
*/
private void initView() {
tv_camera = (TextView) view.findViewById(R.id.tv_camera);
tv_picture_libs = (TextView) view.findViewById(R.id.tv_picture_libs);
tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
}

/**
* 处理点击事件
*
* @param v
*/
@Override
public void onClick(View v) {
Intent intent;
switch (v.getId()) {
case R.id.tv_camera:
// 拍照上传
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 相机捕捉图片的意图
Uri picFileFullName = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg"));
i.putExtra(MediaStore.EXTRA_OUTPUT, picFileFullName);// 指定系统相机拍照保存在imageFileUri所指的位置
mActivity.startActivityForResult(i, CAMERA);
} else {
ToastUtils.show(MyApplication.getCtx(), "请插入内存卡");
}
break;
case R.id.tv_picture_libs:
// 图库上传
intent = new Intent(Intent.ACTION_PICK, null);
// intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
// IMAGE_UNSPECIFIED);
intent.setType(IMAGE_UNSPECIFIED);
mActivity.startActivityForResult(intent, PICTRUE_LIBS);
break;
case R.id.tv_cancel:
// 取消
this.dismiss();
break;
}
}

/**
* 对头像进行裁剪
*
* @param uri
*/
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
// //裁剪图片意图
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// 裁剪框的比例,1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 裁剪后输出图片的尺寸大小
intent.putExtra("outputX", 240);
intent.putExtra("outputY", 240);
// 图片格式
intent.putExtra("outputFormat", "JPEG");
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
mActivity.startActivityForResult(intent, CUTTING);
this.dismiss();
}
}

调用时的代码(当点击头像,准备设置头像之时)

SettingAvatarDialog dialog = new SettingAvatarDialog(this);
dialog.show();

另外重写

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ToastUtils.show(this, "homeactivity返回了");
switch (requestCode) {
case 33:

//拍照返回的结果
if (resultCode == this.RESULT_OK) {
File file = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
if (file != null) {
dialog.startPhotoZoom(Uri.fromFile(file));
}
}else{
dialog.dismiss();
}
break;
case SettingAvatarDialog.PICTRUE_LIBS:

//拍照返回的结果
if (resultCode == this.RESULT_OK) {
Uri uri = data.getData();
if (uri != null) {
dialog.startPhotoZoom(uri);
} else {
dialog.dismiss();
ToastUtils.show(this, "无法加载图片");
}
}else{
dialog.dismiss();
}

break;
case SettingAvatarDialog.CUTTING:
//拍照返回的结果
if (resultCode == this.RESULT_OK) {
Bundle extras = data.getExtras();
if (extras != null) {
final Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);

imageView.setImageBitmap(photo);
}else{
dialog.dismiss();
}
}
break;
default:
break;
}

值得注意的地方

上述代码是在Activity中使用的过程,如果在Fragment中使用时

  1. 在我们重写的onActivityResult()方法中this.RESULTOK改为mActivity.RESULTOK
  2. Fragment和Activity在通过startActivityForResult()方法打开acitivyt时,他俩都可以重写onAcitivityResult(),但是在上述的代码中如果将onAcitivityResult()写到framgnet里面,onAcitivityResult()将不会被正确调用,所以使用fragment的同学们需要找到fragment依附的activity,在其中重写onAcitivityResult()方法
  3. 一般的,常用的方法(将Bitmap对象转为由Base64编码的字符串)
    Bitmap photo = extras.getParcelable("data");
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);
    String base64Photo = Base64.encodeToString(stream.toByteArray(), Base64.DEFAULT);