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;
}
网友评论