Android上传头像,图片剪裁,压缩图片

  • 点击头像的时候开始调用camera()方法
private byte[] mContent = new byte[1024];// 保存照片转换后的字节,用与上传到服务器
 private Bitmap myBitmap;
 private static final int REQUEST_CAMERA = 1;
 private static final int REQUEST_CALENDAR = 2;



public void camera() {
  final CharSequence[] items = { "相册", "拍照" };
  AlertDialog dlg = new AlertDialog.Builder(PersonDataActivity.this)
    .setTitle("选择图片")
    .setItems(items, new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int item) {
      // 这里item是根据选择的方式,

      // 在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法
      if (item == 1) {
       Intent getImageByCamera = new Intent(
         "android.media.action.IMAGE_CAPTURE");
       startActivityForResult(getImageByCamera,
         REQUEST_CAMERA);
      } else {
       Intent getImage = new Intent(
         Intent.ACTION_GET_CONTENT);
       getImage.addCategory(Intent.CATEGORY_OPENABLE);
       getImage.setType("image/jpeg");
       startActivityForResult(getImage, 0);
      }
     }
    }).create();
  dlg.show();
 }

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  ContentResolver resolver = getContentResolver();
  if (requestCode == 0) {
   try {
    Uri originalUri = data.getData();
    startPhotoZoom(originalUri);// 图片剪裁
    Bitmap bm = yasuo(originalUri);
    // Bitmap bm = (Bitmap) data.getExtras().get("data");
    // Bitmap bb = ThumbnailUtils.extractThumbnail(bm, 420, 320);
    Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(
      getContentResolver(), bm, null, null));

    mContent = CameraUtils.readStream(resolver.openInputStream(Uri
      .parse(uri.toString())));
    // 将字节数组转换为ImageView可调用的Bitmap对象
    // myBitmap = CameraUtils.getPicFromBytes(mContent, null);
    // //把得到的图片绑定在控件上显示
    // img_touxiang.setImageBitmap(myBitmap);
    // 处理圆角
    // Bitmap icon = CameraUtils.toRoundCorner(myBitmap);
    // img_touxiang.setImageBitmap(icon);
   } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
   }
  } else if (requestCode == REQUEST_CAMERA) {
   try {
    super.onActivityResult(requestCode, resultCode, data);
    Bundle extras = data.getExtras();
    myBitmap = (Bitmap) extras.get("data");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    mContent = baos.toByteArray();
   } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
   }
   // iv_icon.setImageBitmap(Utils.toRoundCorner(myBitmap, 10));//
   // 把拍摄的照片转成圆角显示在预览控件上
   // Bitmap icon = Utils.toRoundCorner(myBitmap, 65);
   // img_touxiang.setImageBitmap(myBitmap);

  } else if (requestCode == REQUEST_CALENDAR) {
   if (requestCode == RESULT_OK) {

   }
  }
  if (data != null) {
   getImageToView(data);
  }
  // 上传到数据库,并修改页面图像
  HashMap<String, Object> map = service.updateAvatar(uid, mContent,
    "android", String.valueOf(1020));
  if (map != null && !"".equals(map.get("newAvatar").toString())) {
   // 处理返回的新头像地址
   // 重新保存图片
   spf = getSharedPreferences("data", 0);
   Editor e = spf.edit();
   e.putString("avatar", map.get("newAvatar").toString());
   e.commit();

  } else {
   Utils.showToast(PersonDataActivity.this, "上传头像失败");
  }
 }
/**
  * 裁剪图片方法实现
  * 
  * @param uri
  */
 public void startPhotoZoom(Uri uri) {

  Intent intent = new Intent("com.android.camera.action.CROP");
  intent.setDataAndType(uri, "image/*");
  // 设置裁剪
  intent.putExtra("crop", "true");
  // aspectX aspectY 是宽高的比例
  intent.putExtra("aspectX", 1);
  intent.putExtra("aspectY", 1);
  // outputX outputY 是裁剪图片宽高
  intent.putExtra("outputX", 320);
  intent.putExtra("outputY", 320);
  intent.putExtra("return-data", true);
  startActivityForResult(intent, 2);
 }

 /**
  * 保存裁剪之后的图片数据
  * 
  * @param picdata
  */
 private void getImageToView(Intent data) {
  Bundle extras = data.getExtras();
  if (extras != null) {
   Bitmap photo = extras.getParcelable("data");
   Drawable drawable = new BitmapDrawable(photo);
   img_touxiang.setImageDrawable(drawable);
  }
 }

/**

*压缩图片

*

 private Bitmap yasuo(Uri uri) {
  Bitmap bitmap = null;
  try {

   BitmapFactory.Options options = new BitmapFactory.Options();
   options.inJustDecodeBounds = true;
   bitmap = BitmapFactory.decodeStream(this.getContentResolver()
     .openInputStream(uri), null, options);
   int picWidth = options.outWidth;
   int picHeight = options.outHeight;
   WindowManager windowManager = getWindowManager();
   Display display = windowManager.getDefaultDisplay();
   int screenWidth = display.getWidth();
   int screenHeight = display.getHeight();
   options.inSampleSize = 1;
   if (picWidth > picHeight) {
    if (picWidth > screenWidth)
     options.inSampleSize = picWidth / screenWidth;
   } else {
    if (picHeight > screenHeight)
     options.inSampleSize = picHeight / screenHeight;
   }
   options.inJustDecodeBounds = false;
   bitmap = BitmapFactory.decodeStream(this.getContentResolver()
     .openInputStream(uri), null, options);
   img_touxiang.setImageBitmap(bitmap);
   /*
    * if (bitmap.isRecycled() == false) { bitmap.recycle(); }
    */
   System.gc();
  } catch (Exception e1) {
  }
  return bitmap;

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值