android+图片保存+相册读取

经过一天半的比较、运行,终于自己写成、并总结一个集各家所长的好方法~哈哈哈~

 1     public Boolean saveBitmapToFile(Bitmap bitmap, String file_str){
 2 
 3         BufferedOutputStream bufferedOutputStream = null;
 4         Boolean save_status = false;
 5         try{
 6             File file = new File(file_str);
 7 
 8             //下面一段是判断是否该路径存在
 9             int end = file_str.lastIndexOf(File.separator); //返回最后一个"."or "/"的位置
10             String path_str = file_str.substring(0,end); //开始位置,结束位置
11             File file_path = new File(path_str);
12             if(!file_path.exists()){ //判断路径是否存在
13                 file_path.mkdirs(); //文件操作需要权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
14             }
15 
16             file.createNewFile(); //创建文件
17             bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
18             save_status =  bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bufferedOutputStream);
19 
20             if(save_status){ //保存成功更新数据库,让gallery能够读取到
21                 if(hasKitkat()){
22                     MediaScannerConnection.scanFile(WaterMarkActivity.this,
23                             new String[]{file.getAbsolutePath()},
24                             new String[]{"image/*"},
25                             new MediaScannerConnection.OnScanCompletedListener() {
26                                 @Override
27                                 public void onScanCompleted(String s, Uri uri) {
28 
29                                 }
30                             });
31                     scanPhotos(file.getAbsolutePath(),WaterMarkActivity.this);
32                 }else{
33                     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+file.getAbsolutePath())));
34                 }
35 
36             }
37 
38         }catch (Exception e){
39             e.printStackTrace();
40         }finally {
41             if (bufferedOutputStream != null){
42                 try {
43                     bufferedOutputStream.close();
44                 }catch (IOException e){
45                     Log.d("Tag_IO_Exception", e.getMessage());
46                 }
47             }
48         }
49 
50         return save_status;
51     }
52 
53     public static void scanPhotos(String filePath, Context context) {
54         Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
55         Uri uri = Uri.fromFile(new File(filePath));
56         intent.setData(uri);
57         context.sendBroadcast(intent);
58     }
59 
60     public static boolean hasKitkat() {
61         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
62     }

相关链接:

http://bbs.9ria.com/thread-248290-1-1.html

http://www.cnblogs.com/liuhongzhe-5240/archive/2012/08/08/2628289.html

1、以专家为榜样,不必自己重新探索
2、解构技能,找出实现80%效果的那20%
3、不要一心二用
4、练习练习再练习!然后获得即时反馈
5、坚持,不要在低谷期放弃
原文地址:https://www.cnblogs.com/zhongyuan/p/4093439.html