学习了下图片压缩的实现方式,代码很简单,小马就不多讲了,直接上代码:

 

 

 
  
  1. package com.xiaoma.temp.android.demo; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileNotFoundException; 
  5. import java.io.FileOutputStream; 
  6. import java.io.IOException; 
  7.  
  8. import android.app.Activity; 
  9. import android.graphics.Bitmap; 
  10. import android.graphics.BitmapFactory; 
  11. import android.graphics.BitmapFactory.Options; 
  12. import android.graphics.drawable.BitmapDrawable; 
  13. import android.graphics.drawable.Drawable; 
  14. import android.os.Bundle; 
  15. import android.util.Log; 
  16. import android.widget.ImageView; 
  17.  
  18. /**   
  19. * @Title: AndroidTempDemoActivity.java 
  20. * @Package com.xiaoma.temp.android.demo 
  21. * @Description: 压缩测试类 
  22. * @author XiaoMa 
  23. */ 
  24. public class CompressActivity extends Activity{ 
  25.      
  26.       private Bitmap bmap; 
  27.       private ImageView image1; 
  28.       private ImageView image2; 
  29.      
  30.     /** Called when the activity is first created. */ 
  31.     @Override 
  32.     public void onCreate(Bundle savedInstanceState) { 
  33.         super.onCreate(savedInstanceState); 
  34.         setContentView(R.layout.main); 
  35.         init(); 
  36.     } 
  37.      
  38.     private void init(){ 
  39.         image1 = (ImageView)findViewById(R.id.imageView1); 
  40.         image2 = (ImageView)findViewById(R.id.imageView2); 
  41.         Bitmap image2Bit = copressImage("/sdcard/notes.png"); 
  42.         saveCompressPic(image2Bit); 
  43.         Drawable d = new BitmapDrawable(image2Bit); 
  44.         image2.setBackgroundDrawable(d); 
  45.     } 
  46.      
  47.     /** 
  48.      * 如果大家要保存图片的话返回bmap就行了。保存方法也写下面了。 
  49.      * 如果有要用到截剪图片的功能。请留言,小马一起发. 
  50.      * @param imgPath 
  51.      */ 
  52.     @SuppressWarnings("unused"
  53.     private Bitmap copressImage(String imgPath){ 
  54.         File picture = new File(imgPath); 
  55.         Options bitmapFactoryOptions = new BitmapFactory.Options(); 
  56.         //下面这个设置是将图片边界不可调节变为可调节 
  57.         bitmapFactoryOptions.inJustDecodeBounds = true
  58.         bitmapFactoryOptions.inSampleSize = 2
  59.         int outWidth  = bitmapFactoryOptions.outWidth; 
  60.         int outHeight = bitmapFactoryOptions.outHeight; 
  61.         bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(), 
  62.              bitmapFactoryOptions); 
  63.         float imagew = 150
  64.         float imageh = 150
  65.         int yRatio = (int) Math.ceil(bitmapFactoryOptions.outHeight 
  66.                 / imageh); 
  67.         int xRatio = (int) Math 
  68.                 .ceil(bitmapFactoryOptions.outWidth / imagew); 
  69.         if (yRatio > 1 || xRatio > 1) { 
  70.             if (yRatio > xRatio) { 
  71.                 bitmapFactoryOptions.inSampleSize = yRatio; 
  72.             } else { 
  73.                 bitmapFactoryOptions.inSampleSize = xRatio; 
  74.             } 
  75.  
  76.         }  
  77.         bitmapFactoryOptions.inJustDecodeBounds = false
  78.         bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(), 
  79.                 bitmapFactoryOptions); 
  80.         if(bmap != null){                
  81.             //ivwCouponImage.setImageBitmap(bmap); 
  82.             return bmap; 
  83.         } 
  84.         return null
  85.  } 
  86.      
  87.     /** 
  88.      * 保存方法实现 
  89.      * @param bitmap 
  90.      */ 
  91.     private void saveCompressPic(Bitmap bitmap){ 
  92.         File file=new File("/sdcard/kkk/notes2.png"); 
  93.         if(!file.exists()){ 
  94.             file.mkdirs(); 
  95.         } 
  96.         Log.i("KKK""调用咯"+"..."+file.toString()); 
  97.         try { 
  98.             FileOutputStream out=new FileOutputStream(file); 
  99.             if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){ 
  100.                 out.flush(); 
  101.                 out.close(); 
  102.             } 
  103.         } catch (FileNotFoundException e) { 
  104.             e.printStackTrace(); 
  105.         } catch (IOException e) { 
  106.             e.printStackTrace(); 
  107.         } 
  108.     } 

    源码小马也放附件里面了,代码临时写的,很简单,有需要的下载下,不需要的请直接跳过。。吼吼。。有问题请直接批评指点。。谢谢