zoukankan      html  css  js  c++  java
  • Android将图片保存到相册并及时看到

      Android中将图片保存到SD卡中,相册里不会及时出现这张图片,因为没有及时更新其索引,一般需要开机几次。当然我们可以手动更新其索引。

          1,首先将文件保存到SD卡中。

         String filePath = "xxx"; //全路径

                  saveImgToSDcard(filePath);

          2,增加Android 内部媒体索引。

        

    public boolean saveImgToGallery(String filePath) {
    
        boolean sdCardExist = Environment.getExternalStorageState().equals(
    
        android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
    
        if (!sdCardExist)
    
          return false; 
    
          try {
    
            ContentValues values = new ContentValues();
    
            values.put("datetaken", new Date().toString());
    
            values.put("mime_type", "image/jpg");
    
            values.put("_data", filePath);
    
            Application app = DoctorApplication.getInstance();
    
            ContentResolver cr = app.getContentResolver();
    
            cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    
          } catch (Exception e) {
    
            e.printStackTrace();
    
          }
    
          return true;
    
        }
    

        3,刷新filePath的上一级目录

        

    MediaScannerConnection.scanFile(MyLanJingCode.this,new String[] { Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath()+ "/"+ filePath.getParentFile().getAbsolutePath() }, null,null);
    

     

        这样就能及时在相册中看到增加的图片了。

  • 相关阅读:
    arm,iptables: No chain/target/match by that name.
    Windows7-USB-DVD-tool提示不能拷贝文件的处理
    WPF实现WORD 2013墨迹批注功能
    windows下实现屏幕分享(C#)
    Owin WebAPI上传文件
    js 下不同浏览器,new Date转换结果时差
    jquery 动态增加的html元素,初始化设置在id或class上的事件无效
    WPF DataGrid模拟click实现效果
    基于Bootstrap的步骤引导html页面
    XWalkView+html 开发Android应用
  • 原文地址:https://www.cnblogs.com/lsc183/p/4315479.html
Copyright © 2011-2022 走看看