/** * 保存方法 */ 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; }
图片流,bitmap保存

最新推荐文章于 2022-09-14 15:55:47 发布
