Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 保存位图时设置日期_Java_Android_Image_Bitmap - Fatal编程技术网

Java 保存位图时设置日期

Java 保存位图时设置日期,java,android,image,bitmap,Java,Android,Image,Bitmap,我正在使用此功能保存位图 MediaStore.Images.Media.insertImage(getContentResolver(), view.getDrawingCache(), "image.png", "image"); 位图已成功保存,但图像上的日期为1.1.1970 如何设置当前日期?您不能使用MediaStore.Images.Media设置日期。您需要使用此选项 ContentValues values = new ContentValues(); values.put(

我正在使用此功能保存位图

MediaStore.Images.Media.insertImage(getContentResolver(),
view.getDrawingCache(), "image.png", "image");
位图已成功保存,但图像上的日期为1.1.1970


如何设置当前日期?

您不能使用
MediaStore.Images.Media
设置日期。您需要使用此选项

ContentValues values = new ContentValues();
values.put(Media.TITLE, title);
values.put(Media.DESCRIPTION, description); 
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis()); // DATE HERE
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, filepath);

context.getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);

如果您需要任何其他指针,我将查看MediaStore的insertImage,它将图像添加到存储并创建/更新ImageColumns记录

此记录包含一个添加的日期字段,即您要查找的字段。

它还包含一个日期字段,该字段与添加时间无关。它表示日期 在其中拍摄图像,并且几乎总是从图像内部拍摄(更准确地说,是从 它的EXIF数据),因此在将旧图像添加到存储时,不会更改

我怀疑你的问题来自于阅读前一篇而不是后一篇,你应该这样做。 要检索所有图像的此字段,请执行以下操作:

String[] projection = new String[]{
            MediaStore.Images.Media._ID,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Images.Media.DATE_ADDED // <-----------------
    };

    Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

    Cursor cur = managedQuery(images, projection,  "",  null, "");
String[]投影=新字符串[]{
MediaStore.Images.Media.\u ID,
MediaStore.Images.Media.BUCKET\u显示\u名称,

MediaStore.Images.Media.DATE\u ADDED//可以确认在4.1.2设备上,Gallery应用程序也会显示jpg的1970。但是文件浏览器会显示正确的文件时间。因此Gallery应用程序可能会提取Exif信息

在KitKat 4.4.2设备上,Gallery应用程序显示正确的日期。文件浏览器指示的文件时间也正确

照片被保存在../DCIM/Camera中,我感到很惊讶

但不管怎样,jpg总是携带文件时间,因为文件名就是文件时间。这些图片被保存为
140542795171。jpg
翻译为2014年7月15日……而140542795171在1970年之后是毫秒。

为我工作的内容添加信息不仅是添加了
拍摄日期,而且还添加了
日期D

values.put(Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000); // should be in unit of seconds
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis()); // should be in unit of ms
我采用了该代码并使用如下方式:

public void addLastPhotoToGallery(String photoPath) {
    if (!Strings.isNullOrEmpty(photoPath)) {
        ContentValues values = new ContentValues();
        // Add the date meta data to ensure the image is added at the front of the gallery
        values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
        values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
        values.put(MediaStore.MediaColumns.DATA, photoPath);

        getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File photoFile = new File(photoPath);
        Uri contentUri = Uri.fromFile(photoFile);
        mediaScanIntent.setData(contentUri);
        sendBroadcast(mediaScanIntent);
    }
}

您可以使用标准的
MediaStore.Images.Media.insertImage()方法,然后手动更新日期时间:

public void insertInMediaStore(Bitmap bitmap, String title, 
            String description,  ContentResolver contentResolver) {

    // insert to media store
    String photoUriStr = MediaStore.Images.Media.insertImage(
            contentResolver, 
            bitmap, 
            title , 
            description);
    Uri photoUri = Uri.parse(photoUriStr);

    // add datetime
    long now = System.currentTimeMillis() / 1000;
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.DATE_ADDED, now);
    values.put(MediaStore.Images.Media.DATE_MODIFIED, now);
    values.put(MediaStore.Images.Media.DATE_TAKEN, now);
    contentResolver.update(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
            values, 
            MediaStore.Images.Media._ID + "=?", 
            new String [] { ContentUris.parseId(photoUri) + "" });

    // call media scanner to refresh gallery
    Intent scanFileIntent = new Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, photoUri);
    MyApp.getInstance().sendBroadcast(scanFileIntent);
}

图像上的日期
?什么意思?文件时间?Exif时间?嗯,png没有Exif…谢谢。这是帮我的忙。