图片过大,大于1M的情况下上传服务器会很耗时,因此要实现压缩上传并且不失真
String mCurrentPhotoPath;
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 指定存放拍摄照片的位置 File f = createImageFile(); openCameraIntent .putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); startActivityForResult(openCameraIntent, REQUEST_TAKE_PHOTO);
private File createImageFile() { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss"); String timeStamp = format.format(new Date()); String imageFileName = "pic_" + timeStamp + ".jpg"; File appDir = new File(Environment.getExternalStorageDirectory(), "qx"); if (!appDir.exists()) { appDir.mkdir(); } File image = new File(appDir, imageFileName); mCurrentPhotoPath = image.getAbsolutePath(); return image; }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_TAKE_PHOTO: if (resultCode == Activity.RESULT_OK) { // 添加到图库,通知更新 galleryAddPic(this, mCurrentPhotoPath); // ByteArrayOutputStream baos = new ByteArrayOutputStream(); cardIv.setImageBitmap( getSmallBitmap(mCurrentPhotoPath)); } break; } }
public static Bitmap getSmallBitmap(String filePath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, 480, 800); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); }public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; }
public static void galleryAddPic(Context context, String path) { Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File f = new File(path); Uri contentUri = Uri.fromFile(f); mediaScanIntent.setData(contentUri); context.sendBroadcast(mediaScanIntent); }在上传至服务器时进行再次压缩防止一个或多个文件有点大时上传慢或者不清晰
如果显示类似于头像,朋友圈显示的小图片等实现压缩到几十k以下上传,如果展示图片大一点并且后台查看保持清晰时可实现压缩到几百k以下上传
map.put(KeyConstants.Image, getcomImageBase64(getSmallBitmap(mCurrentPhotoPath)));
//压缩成100k以下上传
public static String getcomImageBase64(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 100是压缩率不压缩,如果是30就是压缩70%,压缩后的存放在baos中 int options = 100; while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩 baos.reset();// 重置baos即清空baos options -= 10;// 每次都减少10 bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中 } byte[] bytes = baos.toByteArray(); try { baos.flush(); baos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Base64.encodeToString(bytes, Base64.DEFAULT); }
原文地址:http://www.360doc.com/content/14/0428/17/11800748_372972179.shtml