HarmonyOS 鸿蒙Next 相册获取到的图片uri转(data: string | number | Resource | ArrayBuffer)
HarmonyOS 鸿蒙Next 相册获取到的图片uri转(data: string | number | Resource | ArrayBuffer)
2、如果将uri转成: setResponseData(data: string | number | Resource | ArrayBuffer)所需的入参数
let response = new WebResourceResponse();
response.setResponseData(FileUtil.getRawFileContentStrSync(fileUri1));
response.setResponseEncoding(‘utf-8’);
response.setResponseMimeType(mimeType);
response.setResponseCode(200);
response.setReasonMessage(‘OK’);
response.setResponseIsReady(true);
反复翻阅相关文档没有找到uri或转的方案, 拜托了, 在线等!
import photoAccessHelper from '@ohos.file.photoAccessHelper';
import image from '@ohos.multimedia.image';
import fs from '@ohos.file.fs';
import { buffer } from '@kit.ArkTS';
@Entry
@Component
struct Page2 {
@State resultBase64Str: string = ‘’;
@State getAlbum: string = ‘显示相册中的图片’;
@State pixel: image.PixelMap | undefined = undefined;
@State albumPath: string = ‘’;
@State photoSize: number = 0;
async getPictureFromAlbum() {
// 拉起相册,选择图片
let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
PhotoSelectOptions.maxSelectNumber = 1;
let photoPicker = new photoAccessHelper.PhotoViewPicker();
let photoSelectResult: photoAccessHelper.PhotoSelectResult = await photoPicker.select(PhotoSelectOptions);
this.albumPath = photoSelectResult.photoUris[0];
console.info('albumPath: ’ + this.albumPath)
// 读取图片为buffer
const file = fs.openSync(this.albumPath, fs.OpenMode.READ_WRITE);
this.photoSize = fs.statSync(file.fd).size;
console.info('Photo Size: ’ + this.photoSize);
let buffer1 = new ArrayBuffer(this.photoSize);
fs.readSync(file.fd, buffer1);
let base64Str: string = buffer.from(buffer1).toString(‘base64’)
let resultBase64Str = “data:image/png;base64,” + base64Str
this.resultBase64Str = resultBase64Str
fs.closeSync(file);
// 解码成PixelMap
const imageSource = image.createImageSource(buffer1);
console.log('imageSource: ’ + JSON.stringify(imageSource));
this.pixel = await imageSource.createPixelMap({});
}
build() {
Scroll() {
Column() {
Text(‘获取图片’).onClick(async () => {
await this.getPictureFromAlbum()
}) // Image(this.pixel)
Image(this.resultBase64Str).width(40).height(40)
Text(this.resultBase64Str)
}
}
}
}
楼上arrayBuffer亦可使用
下面思路是提供string图片路径
使用photoview图片选择器的图片 ,只能进行临时使用,需要拷贝一份到沙箱目录在进行上传
async accentSelect(len: number = 1) {
// 创建图库选择器实例
let photoPicker = new photoAccessHelper.PhotoViewPicker();
let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions()
PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
PhotoSelectOptions.maxSelectNumber = 1;
let PhotoSelectResult = await photoPicker.select(PhotoSelectOptions)
let file = fileIo.openSync(PhotoSelectResult.photoUris[0], fileIo.OpenMode.READ_ONLY)
// 此处为jpg图片
let path = getContext().filesDir + '/' + new Date().toLocaleTimeString() + '.jpg'
console.log('z--path--' + JSON.stringify(path))
fileIo.copyFileSync(file.fd, path)
return path
}
可以将图片的uri, 转换为setResponseData可以接受的ArrayBuffer
具体如下:
transitionImage2Buffer(uri: string): ArrayBuffer {
// 读取图片为buffer
const file = fs.openSync(uri, fs.OpenMode.READ_ONLY);
this.photoSize = fs.statSync(file.fd).size;
let imageArraybuffer = new ArrayBuffer(this.photoSize);
fs.readSync(file.fd, imageArraybuffer);
fs.closeSync(file);
return imageArraybuffer
}
在HarmonyOS鸿蒙Next系统中,将相册获取到的图片URI转换为数据格式(如字符串、数字、资源或ArrayBuffer),通常可以通过以下步骤实现:
-
读取URI内容:首先,利用系统提供的API(如
FileProvider
或ContentResolver
),根据URI读取图片文件的二进制数据。 -
转换为ArrayBuffer:读取到的二进制数据可以直接存储到
ArrayBuffer
中。HarmonyOS提供了相应的API来处理这种数据转换。 -
其他格式转换:
- 字符串:如果需要将数据转换为字符串(通常是Base64编码),可以使用Java或JavaScript的编码库。
- 数字:直接转换图片数据为数字通常没有实际意义,除非特指某种编码或哈希值。
- 资源:在鸿蒙系统中,资源通常指应用内嵌入的静态文件。从URI读取的内容不能直接作为资源使用,但可以作为资源内容处理。
示例代码(伪代码):
// 假设uri为图片的URI
Uri uri = ...;
InputStream inputStream = getContentResolver().openInputStream(uri);
byte[] bytes = inputStream.readAllBytes();
ArrayBuffer buffer = new ArrayBuffer(bytes);
// 如需转换为Base64字符串
String base64String = Base64.encodeToString(bytes, Base64.DEFAULT);
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html