HarmonyOS鸿蒙Next中保存图片到相册,请问如何修改这张图片的时间戳
HarmonyOS鸿蒙Next中保存图片到相册,请问如何修改这张图片的时间戳
请问如何修改一张图片的时间戳,我保存一张图片到相册中,图片原本是有时间戳的。但是保存下来后,时间戳成为当前的时间了,我试过下面的方式都不可行,我的应用有申请ohos.permission.WRITE_IMAGEVIDEO权限
方式1
//我先通过保存图片,得到了uri,然后使用下面的方式尝试修改时间戳
try {
let request = photoAccessHelper.MediaAssetChangeRequest.createImageAssetRequest(context,uri)
request.getWriteCacheHandler()
request.setTitle('testHhhh')
let assets =request.getAsset()
assets.set(photoAccessHelper.PhotoKeys.DATE_MODIFIED,"1704334445")
assets.commitModify(() =>{
this.printLog(TAG,"commitModify end ")
})
// helper.applyChanges(request).then(() =>{
// this.printLog(TAG,"applyChanges end ")
// })
} catch (e1) {
this.printLog(TAG,"applyChanges err "+JSON.stringify(e1))
}
方式2
//我先通过保存图片,得到了uri,然后使用下面的方式尝试修改时间戳
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.URI, uri);
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await helper.getAssets(fetchOptions);
let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
photoAsset.set(photoAccessHelper.PhotoKeys.DATE_MODIFIED,"1704334445")
await helper.applyChanges(fetchResult)
更多关于HarmonyOS鸿蒙Next中保存图片到相册,请问如何修改这张图片的时间戳的实战教程也可以访问 https://www.itying.com/category-93-b0.html
使用commitModify替代applyChanges
let photoAsset = await fetchResult.getFirstObject();
// 将时间戳转换为数值类型(Unix时间戳,秒级)
await photoAsset.set(photoAccessHelper.PhotoKeys.DATE_MODIFIED, 1704334445);
await photoAsset.commitModify(); // 关键方法
更多关于HarmonyOS鸿蒙Next中保存图片到相册,请问如何修改这张图片的时间戳的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
看了文档,说是System inner fail.
https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-photoaccesshelper,
希望HarmonyOS能继续优化系统稳定性,减少崩溃和重启的情况。
时间戳不可修改,只能get无法set。
我看文档上有说明,只是修改标题的话时间戳是不会更新的:
我不是想查图片的修改时间。而是我往相册写入了一张图片之后,这张图片的时间,在相册中显示为今天2025.7.17。但是这张图片是来源于用户的相机或者其他设备拍摄的,从服务器下载或者从另一台设备传过来,拍摄日期假设是2024年10月1日,我想把这张照片的时间戳改为2024年10月1日,这样在相册中,用户就可以按照时间顺序查找自己的这些图片。如果几千张照片全都是显示为2025.7.17,那么用户找起来会非常头疼,用户体验不好。
在HarmonyOS Next中修改图片时间戳,需使用@ohos.file.photosLibrary
能力。通过PhotosAsset
获取图片,使用photoAccessHelper
修改时间戳属性。关键代码如下:
import photoAccessHelper from '@ohos.file.photosLibrary';
async function modifyImageTimestamp(uri: string, newDate: Date) {
const phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
const asset = await phAccessHelper.getAssets(uri);
const attributes: photoAccessHelper.PhotoKeys = {
dateAdded: Math.floor(newDate.getTime() / 1000)
};
await phAccessHelper.modify(asset.uri, attributes);
}
注意修改的时间需转换为Unix时间戳(秒级)。文件必须位于相册目录下才可修改。
在HarmonyOS Next中修改图片时间戳的正确方式如下:
- 使用PhotoAccessHelper修改时间戳的关键点:
- 需要先获取到PhotoAsset对象
- 修改DATE_MODIFIED属性时需要使用数值类型的时间戳(单位:秒),而不是字符串
- 推荐使用以下代码实现:
async function modifyImageTimestamp(uri) {
try {
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.URI, uri);
let fetchOptions = {
fetchColumns: [photoAccessHelper.PhotoKeys.DATE_MODIFIED],
predicates: predicates
};
let fetchResult = await photoAccessHelper.getAssets(fetchOptions);
let photoAsset = await fetchResult.getFirstObject();
if (photoAsset) {
// 使用数值类型的时间戳(秒)
photoAsset.set(photoAccessHelper.PhotoKeys.DATE_MODIFIED, 1704334445);
await photoAsset.commitModify();
}
} catch (err) {
console.error('Failed to modify timestamp: ' + JSON.stringify(err));
}
}
- 注意事项:
- 确保应用已申请ohos.permission.WRITE_IMAGEVIDEO权限
- 时间戳参数应为Number类型,表示从1970年1月1日开始的秒数
- 修改后需要调用commitModify()提交更改