Android上传头像,图片剪裁,压缩图片
2016-08-27 17:49
435 查看
点击头像的时候开始调用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; }
相关文章推荐
- Android上传头像,图片剪裁,压缩图片
- Android上传头像,图片剪裁,压缩图片
- Android上传头像,图片剪裁,压缩图片
- <android>头像上传,打开相册 —— 图片压缩——上传成功 (附三星手机适配图片旋转问题)
- Android获取图片、裁剪、压缩、上传头像
- react-native-image-picker在Android上闪退的解决办法(上传头像base64,压缩上传图片)
- Android中相机拍摄照片,以及相册选择图片压缩上传(压缩后保存进SD中)(可用于修改头像等)
- Android图片上传(头像裁切+原图原样)
- Android图片上传(头像裁切+原图原样)
- Android如何压缩图片上传服务端
- iOS 调取本地相册/相机,剪裁图片进行头像上传
- Android 图片上传(头像裁切+原图原样)
- Android-图片选择,压缩,上传,加载的实例 (图片选择,压缩实现 )
- Android图片上传(头像裁切+原图原样)
- android 上传图片前压缩文件
- Android图片处理——压缩、剪裁、圆角、保存
- Android-图片选择,压缩,上传,加载 的实例(图片上传(多图也可),服务器端实现,图片加载(图片轮播实现))
- Android如何实现压缩图片后再上传至服务器
- Android图片选取和剪裁头像的相关问题
- 图片压缩上传 Android