Android下的图片压缩及图片和视频的上传

来源:互联网 发布:手机淘宝宝贝怎么分类 编辑:程序博客网 时间:2024/05/18 17:57

Android开发中上传图片很常见,一般为了节省流量会进行压缩的操作,本篇记录一下压缩和上传的方法。

图片压缩的方法 :

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import java.io.ByteArrayOutputStream;  
  2. import java.io.File;  
  3.   
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.net.Uri;  
  9. import android.os.Environment;  
  10. import android.util.Base64;  
  11.   
  12. public class PictureUtil {  
  13.   
  14.     /** 
  15.      * 把bitmap转换成String 
  16.      *  
  17.      * @param filePath 
  18.      * @return 
  19.      */  
  20.     public static String bitmapToString(String filePath) {  
  21.   
  22.         Bitmap bm = getSmallBitmap(filePath);  
  23.   
  24.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  25.         bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);  
  26.         byte[] b = baos.toByteArray();  
  27.   
  28.         return Base64.encodeToString(b, Base64.DEFAULT);  
  29.   
  30.     }  
  31.   
  32.     /** 
  33.      * 根据路径获得图片并压缩返回bitmap用于显示 
  34.      *  
  35.      * @param imagesrc 
  36.      * @return 
  37.      */  
  38.     public static Bitmap getSmallBitmap(String filePath) {  
  39.         final BitmapFactory.Options options = new BitmapFactory.Options();  
  40.         options.inJustDecodeBounds = true;  
  41.         BitmapFactory.decodeFile(filePath, options);  
  42.   
  43.         // Calculate inSampleSize  
  44.         options.inSampleSize = calculateInSampleSize(options, 480800);  
  45.   
  46.         // Decode bitmap with inSampleSize set  
  47.         options.inJustDecodeBounds = false;  
  48.   
  49.         return BitmapFactory.decodeFile(filePath, options);  
  50.     }  
  51.   
  52.     /** 
  53.      * 计算图片的缩放值 
  54.      *  
  55.      * @param options 
  56.      * @param reqWidth 
  57.      * @param reqHeight 
  58.      * @return 
  59.      */  
  60.     public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {  
  61.         // Raw height and width of image  
  62.         final int height = options.outHeight;  
  63.         final int width = options.outWidth;  
  64.         int inSampleSize = 1;  
  65.   
  66.         if (height > reqHeight || width > reqWidth) {  
  67.   
  68.             // Calculate ratios of height and width to requested height and  
  69.             // width  
  70.             final int heightRatio = Math.round((float) height / (float) reqHeight);  
  71.             final int widthRatio = Math.round((float) width / (float) reqWidth);  
  72.   
  73.             // Choose the smallest ratio as inSampleSize value, this will  
  74.             // guarantee  
  75.             // a final image with both dimensions larger than or equal to the  
  76.             // requested height and width.  
  77.             inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
  78.         }  
  79.   
  80.         return inSampleSize;  
  81.     }  
  82.       
  83. }  

图片上传的代码:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 将图片转成String的形式,进行上传 
  3.      * 
  4.      * @param json 
  5.      * @return  
  6.      * @return String   
  7.      * @author hsx 
  8.      * @time 2014-3-21上午10:47:30 
  9.      */  
  10.     public String sendPost(String json) {  
  11.         try {  
  12.             HttpURLConnection httpcon = (HttpURLConnection) ((new URL(POST_URL)  
  13.                     .openConnection()));  
  14.             httpcon.setDoOutput(true);  
  15.             httpcon.setRequestProperty("Content-Type""application/json");  
  16.             httpcon.setRequestProperty("Accept""application/json");  
  17.             httpcon.setRequestMethod("POST");  
  18.             httpcon.connect();  
  19.   
  20.             byte[] outputBytes = json.getBytes("UTF-8");  
  21.             OutputStream os = httpcon.getOutputStream();  
  22.             os.write(outputBytes);  
  23.             os.close();  
  24.               
  25.             int status = httpcon.getResponseCode();  
  26.             if (status != 200) {  
  27.                 throw new IOException("Post failed with error code " + status);  
  28.             }  
  29.             BufferedReader br = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));  
  30.             StringBuilder sb = new StringBuilder();  
  31.             String line;  
  32.             while ((line = br.readLine()) != null) {  
  33.                 sb.append(line+"\n");  
  34.             }  
  35.             br.close();  
  36.             
  37.             return sb.toString();  
  38.         } catch (IOException e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.         return null;  
  42.     }  

图片压缩的方式还有其他的形式,可以参考一下这篇文字:http://104zz.iteye.com/blog/1694762

下载:http://download.csdn.net/detail/jdsjlzx/9496302

0 0
原创粉丝点击