图片压缩

来源:互联网 发布:js关闭alert弹出框 编辑:程序博客网 时间:2024/04/29 22:31
           BitmapFactory.Options options = new BitmapFactory.Options();  
          options.inJustDecodeBounds = true;              
          //获取这个图片的宽和高  
          Bitmap bitmap = BitmapFactory.decodeFile(filepath,options);
          options.inJustDecodeBounds =false;  
          //计算缩放比  
          int be = (int)(options.outHeight / (float)65);  
          if(be <= 0)  
               be =1;  
          options.inSampleSize =be;  
          //重新读入图片
          bitmap = BitmapFactory.decodeFile(filepath,options);  
          int w = bitmap.getWidth();  
          int h=bitmap.getHeight();              
          //保存入sdCard  
          File tp=new File(文件目录);
          if(!tp.exists())
          {
          tp.mkdirs();
          }
         String newfile=文件地址;
          File file2= new File(newfile);  
          try {  
           FileOutputStream out = new FileOutputStream(file2);  
           if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){  
               out.flush();  
               out.close();  
           }             
           } catch (Exception e) {  
           // TODO: handle exception  
        e.printStackTrace();
             }  
原创粉丝点击