图片压缩上传
来源:互联网 发布:淘宝好评卡片 编辑:程序博客网 时间:2024/06/05 19:43
市场上手机像素普遍比较高,拍的相片较大,上传原始图片到服务器耗时耗流量,所以要对图片进行压缩后再进行上传具体流程如下
1、通过文件路径获取图片转化为bitmap对象
/** * 通过路径转为bitMap对象 * @param imgPath * @return */ public static Bitmap getBitmap(String imgPath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;//只解析图片边沿,获取宽高 BitmapFactory.decodeFile(imgPath, options); // 计算缩放比 转为320*480大小图片 options.inSampleSize = calculateInSampleSize(options, 320, 480); // 完整解析图片返回bitmap options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(imgPath, options); }计算图片大小的方法
/** * 计算图片大小 * @param options * @param reqWidth * @param reqHeight * @return */ public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; }
2、开启线程对图片进行压缩
/** * 压缩文件 * @param bmp * @param file */ public static void compressBmpToFile(final Bitmap bmp, final File file){ //开启子线程进行压缩 new Thread(new Runnable() { @Override public void run() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int options = 60; bmp.compress(Bitmap.CompressFormat.JPEG, options, baos); //压缩至100k以内 while (baos.toByteArray().length / 1024 > 100) { baos.reset(); options -= 10; bmp.compress(Bitmap.CompressFormat.JPEG, options, baos); } try { FileOutputStream fos = new FileOutputStream(file); fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }).start(); }3、设置压缩图片存储路径
/** * 获取sd卡路径 */ public static String getSdcardPath() { String path = Environment.getExternalStorageDirectory() + File.separator + "edaibu" + File.separator; File file = new File(path); if (!file.exists()) { file.mkdirs(); } return path; }
Activity代码中调用返回file类型
/** * 压缩图片 * @param imagePath */ private File compressImg(String imagePath) { LogUtils.e("压缩前长度:"+new File(imagePath).length()); Bitmap bitmap= CompressImageUtil.getBitmap(imagePath); final String path=CompressImageUtil.getSdcardPath()+index+"compress.jpg"; File files=new File(path); CompressImageUtil.compressBmpToFile(bitmap,files); LogUtils.e("压缩后的图片长度:"+files.length()); return files; }最后记得在图片上传成功后将压缩的图片进行删除
String compressedPicDir =Environment.getExternalStorageDirectory() + File.separator + "edaibu"; //删除压缩后的图片 destroyPhoto(compressedPicDir);
/** * 销毁文件夹图片 */ @TargetApi(Build.VERSION_CODES.KITKAT) private void destroyPhoto(String path) { LogUtils.e("销毁拍照图片"); //里面输入特定目录 File file = new File(path); File temp; if (file.isDirectory() && file.listFiles().length > 0) { File[] fileList = file.listFiles(); for (int i = 0; i < fileList.length; i++) { temp = fileList[i]; temp.delete(); } } }
阅读全文
0 0
- 图片压缩上传 上传 压缩
- 上传图片按比例压缩
- 图片上传压缩处理
- 图片上传之压缩
- 图片压缩上传
- 图片压缩上传
- 图片上传压缩处理
- android图片压缩上传
- 图片上传之压缩
- 图片上传之压缩
- Android 图片压缩上传
- 图片压缩 ios 上传
- 图片上传压缩
- 图片上传压缩处理
- PHP压缩上传图片
- 图片上传、压缩
- 图片上传压缩处理
- html5 压缩图片上传
- Linux下线程的概念和使用
- gaoKao2Coder4Ever
- golang写入文件操作
- pat1061-1070
- 建立软件工程之动态模型(状态图)步骤
- 图片压缩上传
- 网络编程步骤
- 【脚本语言系列】关于Python异常处理,你需要知道的事
- codeforces 814A
- 2016百度-罪犯转移-Java
- java中volatile关键字的含义
- R语言使用merge函数匹配数据(vlookup,join)
- 漫谈Java理念的转变与JVM的五大优势
- 《演讲的力量》读书笔记