压缩图片搞的我唠嗑疼,有的从网上找的压根不能用。。。
下面看看我整理的吧:
点击头像的时候开始调用camera()方法
1 private byte[] mContent = new byte[1024];// 保存照片转换后的字节,用与上传到服务器 2 private Bitmap myBitmap; 3 private static final int REQUEST_CAMERA = 1; 4 private static final int REQUEST_CALENDAR = 2; 5 6 7 8 public void camera() { 9 final CharSequence[] items = { "相册", "拍照" }; 10 AlertDialog dlg = new AlertDialog.Builder(PersonDataActivity.this) 11 .setTitle("选择图片") 12 .setItems(items, new DialogInterface.OnClickListener() { 13 public void onClick(DialogInterface dialog, int item) { 14 // 这里item是根据选择的方式, 15 16 // 在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法 17 if (item == 1) { 18 Intent getImageByCamera = new Intent( 19 "android.media.action.IMAGE_CAPTURE"); 20 startActivityForResult(getImageByCamera, 21 REQUEST_CAMERA); 22 } else { 23 Intent getImage = new Intent( 24 Intent.ACTION_GET_CONTENT); 25 getImage.addCategory(Intent.CATEGORY_OPENABLE); 26 getImage.setType("image/jpeg"); 27 startActivityForResult(getImage, 0); 28 } 29 } 30 }).create(); 31 dlg.show(); 32 } 33 34 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 35 super.onActivityResult(requestCode, resultCode, data); 36 ContentResolver resolver = getContentResolver(); 37 if (requestCode == 0) { 38 try { 39 Uri originalUri = data.getData(); 40 startPhotoZoom(originalUri);// 图片剪裁 41 Bitmap bm = yasuo(originalUri); 42 // Bitmap bm = (Bitmap) data.getExtras().get("data"); 43 // Bitmap bb = ThumbnailUtils.extractThumbnail(bm, 420, 320); 44 Uri uri = Uri.parse(MediaStore.Images.Media.insertImage( 45 getContentResolver(), bm, null, null)); 46 47 mContent = CameraUtils.readStream(resolver.openInputStream(Uri 48 .parse(uri.toString()))); 49 // 将字节数组转换为ImageView可调用的Bitmap对象 50 // myBitmap = CameraUtils.getPicFromBytes(mContent, null); 51 // //把得到的图片绑定在控件上显示 52 // img_touxiang.setImageBitmap(myBitmap); 53 // 处理圆角 54 // Bitmap icon = CameraUtils.toRoundCorner(myBitmap); 55 // img_touxiang.setImageBitmap(icon); 56 } catch (Exception e) { 57 // TODO: handle exception 58 e.printStackTrace(); 59 } 60 } else if (requestCode == REQUEST_CAMERA) { 61 try { 62 super.onActivityResult(requestCode, resultCode, data); 63 Bundle extras = data.getExtras(); 64 myBitmap = (Bitmap) extras.get("data"); 65 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 66 myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 67 mContent = baos.toByteArray(); 68 } catch (Exception e) { 69 // TODO: handle exception 70 e.printStackTrace(); 71 } 72 // iv_icon.setImageBitmap(Utils.toRoundCorner(myBitmap, 10));// 73 // 把拍摄的照片转成圆角显示在预览控件上 74 // Bitmap icon = Utils.toRoundCorner(myBitmap, 65); 75 // img_touxiang.setImageBitmap(myBitmap); 76 77 } else if (requestCode == REQUEST_CALENDAR) { 78 if (requestCode == RESULT_OK) { 79 80 } 81 } 82 if (data != null) { 83 getImageToView(data); 84 } 85 // 上传到数据库,并修改页面图像 86 HashMap<String, Object> map = service.updateAvatar(uid, mContent, 87 "android", String.valueOf(1020)); 88 if (map != null && !"".equals(map.get("newAvatar").toString())) { 89 // 处理返回的新头像地址 90 // 重新保存图片 91 spf = getSharedPreferences("data", 0); 92 Editor e = spf.edit(); 93 e.putString("avatar", map.get("newAvatar").toString()); 94 e.commit(); 95 96 } else { 97 Utils.showToast(PersonDataActivity.this, "上传头像失败"); 98 } 99 }
1 /** 2 * 裁剪图片方法实现 3 * 4 * @param uri 5 */ 6 public void startPhotoZoom(Uri uri) { 7 8 Intent intent = new Intent("com.android.camera.action.CROP"); 9 intent.setDataAndType(uri, "image/*"); 10 // 设置裁剪 11 intent.putExtra("crop", "true"); 12 // aspectX aspectY 是宽高的比例 13 intent.putExtra("aspectX", 1); 14 intent.putExtra("aspectY", 1); 15 // outputX outputY 是裁剪图片宽高 16 intent.putExtra("outputX", 320); 17 intent.putExtra("outputY", 320); 18 intent.putExtra("return-data", true); 19 startActivityForResult(intent, 2); 20 } 21 22 /** 23 * 保存裁剪之后的图片数据 24 * 25 * @param picdata 26 */ 27 private void getImageToView(Intent data) { 28 Bundle extras = data.getExtras(); 29 if (extras != null) { 30 Bitmap photo = extras.getParcelable("data"); 31 Drawable drawable = new BitmapDrawable(photo); 32 img_touxiang.setImageDrawable(drawable); 33 } 34 } 35 36 /** 37 38 *压缩图片 39 40 * 41 42 private Bitmap yasuo(Uri uri) { 43 Bitmap bitmap = null; 44 try { 45 46 BitmapFactory.Options options = new BitmapFactory.Options(); 47 options.inJustDecodeBounds = true; 48 bitmap = BitmapFactory.decodeStream(this.getContentResolver() 49 .openInputStream(uri), null, options); 50 int picWidth = options.outWidth; 51 int picHeight = options.outHeight; 52 WindowManager windowManager = getWindowManager(); 53 Display display = windowManager.getDefaultDisplay(); 54 int screenWidth = display.getWidth(); 55 int screenHeight = display.getHeight(); 56 options.inSampleSize = 1; 57 if (picWidth > picHeight) { 58 if (picWidth > screenWidth) 59 options.inSampleSize = picWidth / screenWidth; 60 } else { 61 if (picHeight > screenHeight) 62 options.inSampleSize = picHeight / screenHeight; 63 } 64 options.inJustDecodeBounds = false; 65 bitmap = BitmapFactory.decodeStream(this.getContentResolver() 66 .openInputStream(uri), null, options); 67 img_touxiang.setImageBitmap(bitmap); 68 /* 69 * if (bitmap.isRecycled() == false) { bitmap.recycle(); } 70 */ 71 System.gc(); 72 } catch (Exception e1) { 73 } 74 return bitmap; 75 76 }