使用photoAccessHelper.PhotoViewPicker选取图片(或视频),根据资源uri将资源写入到沙箱路径下。photoAccessHelper.MediaAssetDataHandler接口的代理方法onDataPrepared中this是undefined,这样就导致无法获取到沙箱路径,无法把arraybuffer保存到沙箱路径下。
代码:
class MediaDataHandler implements photoAccessHelper.MediaAssetDataHandler<ArrayBuffer> {
private toPath: string;
constructor(toPath: string) {
this.toPath = toPath;
}
onDataPrepared(data: ArrayBuffer, map?: Map<string, string>) {
if (data === undefined) {
console.error('Error occurred when preparing data');
return;
}
console.info('on image data prepared');
// 应用自定义对资源数据的处理逻辑
// 这里this是undefined
IO.writeContentsSync(this.toPath, data);
}
}
在类最外层定义目标路径destPath赋值可以正常保存文件:
在MediaAssetDataHandler中未能正确赋值,说明MediaAssetDataHandler不支持这种方式赋值啊,目前用全局的。