HarmonyOS 鸿蒙Next使用cameraPicker拍照后照片不希望出现在相册中

HarmonyOS 鸿蒙Next使用cameraPicker拍照后照片不希望出现在相册中 我们项目中,使用cameraPicker进行拍照,但发现,拍照后的照片会出现在相册中,是否有这样一个选项,使cameraPicker拍照后,相册不会出现刚拍的照片?

2 回复

照片不存相册可参照:

const context2 = getContext(this);
const imageSourceApi: image.ImageSource = image.createImageSource(this.combinePath);
let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 };
const filePath: string = context2.filesDir + "/image_source.jpg";
let file2 = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
const imagePackerApi: image.ImagePacker = image.createImagePacker();
imagePackerApi.packToFile(imageSourceApi, file2.fd, packOpts, (err: BusinessError) => {
if (err) {
console.error(`Failed to pack the image to file.code ${err.code},message is ${err.message}`);
} else {
console.info('Succeeded in packing the image to file.');
}
})

存储在自定义的路径,参考demo:

import picker from '@ohos.multimedia.cameraPicker'
import camera from '@ohos.multimedia.camera';
import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';
import fileuri from '@ohos.file.fileuri';
import fs from '@ohos.file.fs';
let mContext = getContext(this) as common.Context;
class CameraPosition {
cameraPosition : camera.CameraPosition
saveUri :string
constructor(cameraPosition : camera.CameraPosition,saveUri:string) {
this.cameraPosition = cameraPosition
this.saveUri = saveUri
}
}
let pathDir = getContext().filesDir;
let filePath = pathDir + `${new Date().getTime()}.jpg`
fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE);
let uri = fileuri.getUriFromPath(filePath);
async function demo() {
try {
let pickerProfile = new CameraPosition(camera.CameraPosition.CAMERA_POSITION_BACK,uri)
//前置摄像机传CAMERA_POSITION_FRONT,后置摄像机传CAMERA_POSITION_BACK,saveuri传想存到对应沙箱的uri
let pickerResult: picker.PickerResult = await picker.pick(mContext,
[_picker.PickerMediaType.PHOTO, picker.PickerMediaType.VIDEO], pickerProfile);
console.log("the pick pickerResult is:" + JSON.stringify(pickerResult));
} catch (error) {
let err = error as BusinessError;
console.error(`the pick call failed. error code: ${err.code}`);
}
}
@Entry
@Component
struct IndexPage{
build(){
Column(){
Button('拉起后置摄像头').onClick(() =>{
demo()
})
}
}
}

更多关于HarmonyOS 鸿蒙Next使用cameraPicker拍照后照片不希望出现在相册中的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙Next)中,使用cameraPicker拍照后,默认情况下照片会保存到系统相册中。如果你不希望照片出现在相册中,可以通过以下方式实现:

  1. 使用临时文件存储:拍照后,将照片保存到应用的私有目录中,而不是系统相册目录。鸿蒙提供了Context.getFilesDir()方法,可以获取应用的私有文件目录,将照片保存到这个目录下,照片将不会出现在系统相册中。

  2. 删除系统相册中的照片:如果你已经将照片保存到系统相册中,可以通过MediaStore API查找并删除这张照片。首先获取照片的URI,然后使用ContentResolver.delete()方法将其从系统相册中删除。

  3. 使用ImageReceiver捕获图像:如果你不需要使用cameraPicker,可以考虑使用ImageReceiver来捕获图像数据,并将图像数据直接保存到应用的私有目录中,这样照片不会进入系统相册。

通过以上方法,你可以控制拍照后照片是否出现在系统相册中。

回到顶部