Android--将图片存放到我们本地

代码里面有详细的解释,我就不多说了

 1 //处理并保存图像
 2     private File dealPhoto(Bitmap photo){
 3         FileOutputStream fileOutputStream = null;
 4         //图片的名称,已时间命名
 5         SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); //时间格式
 6         Date date = new Date(System.currentTimeMillis());   //当前时间
 7         String photoName = format.format(date);      //格式化名称
 8         //图片存放地址
 9         File saveDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); //保存到系统图库中
10         File file = new File(saveDir,photoName+".jpg");  //在这个路径下生成这么一个文件生成这么一个文件
11         try {
12             fileOutputStream = new FileOutputStream(file);  //将本地图片读成流
13             photo.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream);   //保存图片到本地,100是压缩比率,表示100%压缩
14 
15         } catch (FileNotFoundException e) {
16             e.printStackTrace();
17         }finally {
18             if (photo != null && photo.isRecycled()){
19                 photo.recycle();   //释放内存
20             }
21             try {
22                 if (fileOutputStream != null) {
23                     fileOutputStream.close();
24                 }
25             } catch (IOException e) {
26                 e.printStackTrace();
27             }
28         }
29         return file;
30     }
原文地址:https://www.cnblogs.com/819158327fan/p/4906404.html