android 上传图片过大处理

在开发Android应用时,会经常上传图片到服务器,会经常出现上传图片失败和时间过长的问题

为了解决这个问题,决定把照片进行压缩后上保存后本地缓存文件夹后再上传

使用方法:

//压缩后得到新的图片路径

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. BitmapUtils.compressImageUpload    


// 上传成功后删除缓存文件

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. BitmapUtils.deleteCacheFile()  


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class BitmapUtils {  
  2.   
  3.     /** 
  4.      * 质量压缩方法 
  5.      *  
  6.      * @param image 
  7.      * @return 
  8.      */  
  9.     private static Bitmap compressImage(Bitmap image) {  
  10.   
  11.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  12.         image.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  13.         int options = 100;  
  14.         while (baos.toByteArray().length / 1024 > 100) {   
  15.             baos.reset();  
  16.             image.compress(Bitmap.CompressFormat.JPEG, options, baos);  
  17.             options -= 10;  
  18.         }  
  19.         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  
  20.         Bitmap bitmap = BitmapFactory.decodeStream(isBm, nullnull);  
  21.         return bitmap;  
  22.     }  
  23.   
  24.     /** 
  25.      * 图片按比例大小压缩方法(根据路径获取图片并压缩) 
  26.      *  
  27.      * @param srcPath 
  28.      * @return 
  29.      */  
  30.     private static Bitmap getImage(String srcPath) {  
  31.         BitmapFactory.Options newOpts = new BitmapFactory.Options();  
  32.         newOpts.inJustDecodeBounds = true;  
  33.         Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空  
  34.   
  35.         newOpts.inJustDecodeBounds = false;  
  36.         int w = newOpts.outWidth;  
  37.         int h = newOpts.outHeight;  
  38.         // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为  
  39.         float hh = 800f;// 这里设置高度为800f  
  40.         float ww = 480f;// 这里设置宽度为480f  
  41.         // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可  
  42.         int be = 1;// be=1表示不缩放  
  43.         if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放  
  44.             be = (int) (newOpts.outWidth / ww);  
  45.         } else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放  
  46.             be = (int) (newOpts.outHeight / hh);  
  47.         }  
  48.         if (be <= 0)  
  49.             be = 1;  
  50.         newOpts.inSampleSize = be;// 设置缩放比例  
  51.         // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了  
  52.         bitmap = BitmapFactory.decodeFile(srcPath, newOpts);  
  53.         return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩  
  54.     }  
  55.   
  56.     /** 
  57.      * 将压缩的bitmap保存到SDCard卡临时文件夹,用于上传 
  58.      *  
  59.      * @param filename 
  60.      * @param bit 
  61.      * @return 
  62.      */  
  63.     private static String saveMyBitmap(String filename, Bitmap bit) {  
  64.         String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath()+"/laopai/";  
  65.         String filePath = baseDir + filename;  
  66.         File dir = new File(baseDir);  
  67.         if (!dir.exists()) {  
  68.             dir.mkdir();  
  69.         }  
  70.   
  71.         File f = new File(filePath);  
  72.         try {  
  73.             f.createNewFile();  
  74.             FileOutputStream fOut = null;  
  75.             fOut = new FileOutputStream(f);  
  76.             bit.compress(Bitmap.CompressFormat.PNG, 100, fOut);  
  77.             fOut.flush();  
  78.             fOut.close();  
  79.         } catch (IOException e1) {  
  80.             e1.printStackTrace();  
  81.         }  
  82.   
  83.         return filePath;  
  84.     }  
  85.   
  86.     /** 
  87.      * 压缩上传路径 
  88.      * @param path 
  89.      * @return 
  90.      */  
  91.     public static String compressImageUpload(String path) {  
  92.         String filename = path.substring(path.lastIndexOf("/") + 1);  
  93.         Bitmap image = getImage(path);  
  94.         return saveMyBitmap(filename, image);  
  95.     }  
  96.   
  97.   
  98.     /** 
  99.      * 清除缓存文件 
  100.      */  
  101.     public static void deleteCacheFile(){  
  102.         File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/laopai/");  
  103.         RecursionDeleteFile(file);  
  104.     }  
  105.     /** 
  106.      * 递归删除 
  107.      */  
  108.     private static void RecursionDeleteFile(File file){  
  109.         if(file.isFile()){  
  110.             file.delete();  
  111.             return;  
  112.         }  
  113.         if(file.isDirectory()){  
  114.             File[] childFile = file.listFiles();  
  115.             if(childFile == null || childFile.length == 0){  
  116.                 file.delete();  
  117.                 return;  
  118.             }  
  119.             for(File f : childFile){  
  120.                 RecursionDeleteFile(f);  
  121.             }  
  122.             file.delete();  
  123.         }  
  124.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值