bitmap的几个用法

1.通过存放图片的路径,压缩图片,使用方法compress()

public static Bitmap getSmallBitmap(String filePath) {  

        
        final BitmapFactory.Options options = new BitmapFactory.Options();  
        options.inJustDecodeBounds = true;  
        BitmapFactory.decodeFile(filePath, options);  
 
        // Calculate inSampleSize  
        options.inSampleSize = calculateInSampleSize(options,240,240);  
 
        // Decode bitmap with inSampleSize set  
        options.inJustDecodeBounds = false;  
          
        Bitmap bm = BitmapFactory.decodeFile(filePath, options);  
        if(bm == null){  
            return  null;  
        }  
       
        ByteArrayOutputStream baos = null ;  
        try{  
            baos = new ByteArrayOutputStream();  
            bm.compress(Bitmap.CompressFormat.JPEG,50, baos);  
              
        }finally{  
            try {  
                if(baos != null)  
                    baos.close() ;  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return bm ;  
 

    } 



private static int calculateInSampleSize(BitmapFactory.Options options,  
            int reqWidth, int reqHeight) {  
        // Raw height and width of image  
        final int height = options.outHeight;  
        final int width = options.outWidth;  
        int inSampleSize = 1;  
 
        if (height > reqHeight || width > reqWidth) {  
 
            // Calculate ratios of height and width to requested height and  
            // width  
            final int heightRatio = Math.round((float) height  
                    / (float) reqHeight);  
            final int widthRatio = Math.round((float) width / (float) reqWidth);  
 
            // Choose the smallest ratio as inSampleSize value, this will  
            // guarantee  
            // a final image with both dimensions larger than or equal to the  
            // requested height and width.  
            inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;  
        }  
 
        return inSampleSize;  
    } 


2.把图片保存到指定的路径

/***
 * 把bitmap保存到指定路径
 */
    private void saveCroppedImage(Bitmap bmp,String imgName) {
        File file = new File("/sdcard/myFolder");
        if (!file.exists())
            file.mkdir();
 
      //  file = new File("/sdcard/temp.jpg".trim());
        file = new File("/sdcard/"+imgName.trim());///sdcard/temp.jpg    //这里imgName  替代  temp.jpg
        urls="/sdcard/myFolder";
        
        String fileName = file.getName();//temp.jpg
        String mName = fileName.substring(0, fileName.lastIndexOf("."));//temp
        String sName = fileName.substring(fileName.lastIndexOf("."));//.jpg
 
        // /sdcard/myFolder/temp_cropped.jpg
        String newFilePath = "/sdcard/myFolder" + "/" + mName + "_c" + sName;//  /sdcard/myFolder/temp_cropped.jpg
      
        
        file = new File(newFilePath);//   /sdcard/myFolder/temp_cropped.jpg
        try {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(CompressFormat.JPEG, 50, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }

3.

/**
     * 从指定路径取出图片的路径集合
     * 获取SDCard中某个目录下图片路径集合
     * @param strPath
     * @return
     */
     public List<String> getPictures(final String strPath) {
        List<String> list = new ArrayList<String>();
        File file = new File(strPath);
        File[] allfiles = file.listFiles();
        if (allfiles == null) {
          return null;
        }
        for(int k = 0; k < allfiles.length; k++) {
          final File fi = allfiles[k];
          if(fi.isFile()) {
                  int idx = fi.getPath().lastIndexOf(".");
                  if (idx <= 0) {
                      continue;
                  }
                  String suffix = fi.getPath().substring(idx);
                  if (suffix.toLowerCase().equals(".jpg") ||
                      suffix.toLowerCase().equals(".jpeg") ||
                      suffix.toLowerCase().equals(".bmp") ||
                      suffix.toLowerCase().equals(".png") ||
                      suffix.toLowerCase().equals(".gif") ) {
                      list.add(fi.getPath());
                  }
          }
       }
       return list;
     }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值