android实现一张或多张图片压缩并保持清晰上传
来源:互联网 发布:汤姆汉克斯的地位知乎 编辑:程序博客网 时间:2024/04/29 15:24
图片过大,大于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
0 0
- android实现一张或多张图片压缩并保持清晰上传
- 上传多张图片并压缩
- 上传一张或多张图片(使用volley框架)
- php两张图片合并一张,加文字,并压缩
- retrofit 上传一张或多张图片复杂但很有效的方法
- android递归压缩上传多张图片到七牛
- java图片处理——多张图片合成一张Gif图片并播放或Gif拆分成多张图片
- java图片处理——多张图片合成一张Gif图片并播放或Gif拆分成多张图片
- thinkphp上传多张图片只能上传一张的问题
- weui实现多张图片上传,单个预览并删除
- 压缩并用 Alamofire 上传多张图片
- Android Retrofit 2.0框架上传图片解决方案(一张与多张的处理)
- Android Retrofit 2.0框架上传图片解决方案(一张与多张的处理)
- Android Retrofit 2.0框架上传图片解决方案(一张与多张的处理)
- Android Retrofit 2.0框架上传图片解决方案(一张与多张的处理)
- Android Retrofit 2.0框架上传图片解决方案(一张与多张的处理)
- Android 上传多张图片
- 异步提交一张或多张图片和表单数据
- java多态-示例
- 如何绘制caffe网络训练曲线
- 利用两个BrocastReceiver来保证同时只有一个实例在运行
- 性能测试计划的纲要
- React生命周期
- android实现一张或多张图片压缩并保持清晰上传
- 剑指offer39--翻转单词和左旋转字符串
- PushNotification消息推送逻辑
- Android 在代码中为textview设置自定义的颜色
- 013--Floyd算法-动态规划-《算法设计技巧与分析》M.H.A学习笔记
- Spring jar 包作用
- C#dC# 简单网页外挂实例
- Android adt bundle 开发环境配置及第一个“Hello world”程序运行
- Leetcode Valid Sudoku