图片流,bitmap保存

图片处理:Bitmap的保存操作
本文主要介绍了在Android中如何处理图片流并将其保存为Bitmap对象,详细讲解了Bitmap的保存过程,包括选择合适的图片格式、压缩质量和大小优化等关键步骤。
/**
 * 保存方法
 */
public void saveBitmap(Bitmap bitmap) {
   Long time = System.currentTimeMillis();
   File f = new File(Environment.getExternalStorageDirectory() + "/maketion/"+String.valueOf(time)+".png");
   picturePath = f.getPath();
   if (f.exists()) {
      f.delete();
   }
   try {
      FileOutputStream out = new FileOutputStream(f);
      bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
      out.flush();
      out.close();
   } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }

}

 Bitmap save(byte[] data){
   if(data!=null){
      //解析生成相机返回的图片
      Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);
      //生成缩略图
      Bitmap thumbnail= ThumbnailUtils.extractThumbnail(bm, 213, 213);
      //创建目录
      File fileDir = new File(getSDPath() + "/maketion");
      makeDir(fileDir);
      File majorDir = new File(getSDPath() + "/major");
      makeDir(majorDir);
      //产生新的文件名
      String imgName=createFileNmae(".jpg");
      picturePath=getSDPath() + "/maketion"+File.separator+imgName;
      String thumbPath=getSDPath() + "/major"+ File.separator+imgName;


      File file = new File(picturePath);
      if (!file.exists()) {
         try {
            file.createNewFile();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
      File thumFile=new File(thumbPath);
      if (!thumFile.exists()) {
         try {
            thumFile.createNewFile();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
      try{
         //存图片大图
         FileOutputStream fos=new FileOutputStream(file);
         ByteArrayOutputStream bos=compress(bm);
         fos.write(bos.toByteArray());
         fos.flush();
         fos.close();
         //存图片小图
         BufferedOutputStream bufferos=new BufferedOutputStream(new FileOutputStream(thumFile));
         thumbnail.compress(Bitmap.CompressFormat.JPEG, 50, bufferos);
         bufferos.flush();
         bufferos.close();
         return bm;
      }catch(Exception e){
         Toast.makeText(this, "解析相机返回流失败", Toast.LENGTH_SHORT).show();

      }
   }else{
      Toast.makeText(this, "拍照失败,请重试", Toast.LENGTH_SHORT).show();
   }
   return null;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安卓学习乐园

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值