Android 保存到系统相册时间显示为1970 记录一下

String path = Environment.getExternalStorageDirectory().getPath() + "/.chang"; 图片路径
File docFile = new File(path);
if (!docFile.exists()) {
       docFile.mkdirs();
   }
String fileName = System.currentTimeMillis()+“.jpg”图片名称
  File file = new File(docFile.getPath(), fileName);
try { 压缩保存图片
       FileOutputStream outputStream = new FileOutputStream(file);
       bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
      outputStream.flush();
       outputStream.close();
       
  
     } catch (Exception e) {
            e.printStackTrace();
    }
这样写可以更新到系统相册 但是查看详情时 图片显示1970年1月1日
MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));

正确修改图片详情显示时间
String insertImage = MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
File file1 = new File(getRealPathFromURI(Uri.parse(insertImage),context));
updatePhotoMedia(file1,context);

//更新图库
private static void updatePhotoMedia(File file ,Context context){
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    intent.setData(Uri.fromFile(file));
    context.sendBroadcast(intent);
}
//得到绝对地址
private static String getRealPathFromURI(Uri contentUri,Context context) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String fileStr = cursor.getString(column_index);
    cursor.close();
    return fileStr;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容