HarmonyOS鸿蒙Next中将图片存入数据库,不存地址
HarmonyOS鸿蒙Next中将图片存入数据库,不存地址 我想将图片存入数据库,但是不存地址,我开始是看到有个帖子是将图片转为base64格式,但是现在没有了,有具体的demo吗,从选择图片,到转为base64,在解码显示的
通过photoAccessHelper访问和获取相册中的媒体文件,使用PhotoViewPicker选择需要转换的图片,返回图库选择后的结果集,读取图片为ArrayBuffer后转换为base64字符串。参考代码如下:
import photoAccessHelper from '@ohos.file.photoAccessHelper';
import image from '@ohos.multimedia.image';
import fs from '@ohos.file.fs';
import { buffer } from '@kit.ArkTS';
@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);
// 转换为base64
let base64Str: string = buffer.from(buffer1).toString('base64')
let resultBase64Str = "data:image/png;base64," + base64Str
this.resultBase64Str = resultBase64Str
fs.closeSync(file);
}
build() {
Scroll() {
Column() {
Text('获取图片')
.onClick(async () => {
await this.getPictureFromAlbum()
})
Image(this.resultBase64Str).width(40).height(40)
Text(this.resultBase64Str)
}
}
}
}
更多关于HarmonyOS鸿蒙Next中将图片存入数据库,不存地址的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
//从数据库获取到Unit8Array图片,转换成base64显示到UI
DBUtil.queryPostByUid(this.uid).then((result) => {
this.result = result
if (this.result[0].titlePic !== undefined ) {
let buf = buffer.from(this.result[0].titlePic);
//得到base64
let base64Str = buf.toString('base64')
this.imageBase64 = `data:image/png;base64,${base64Str}`;
console.info("This titlePic Base64 is " + this.imageBase64)
console.info("This titlePic is " + this.result[0].titlePic)
}
})
在HarmonyOS鸿蒙Next中,将图片直接存入数据库而不存储地址,可以通过将图片转换为字节数组(byte array)的方式实现。具体步骤如下:
-
图片转换为字节数组:使用
Image
组件加载图片,然后通过ImageSource
将图片转换为字节数组。可以使用ImageSource.createFromPixelMap
方法将PixelMap
对象转换为字节数组。 -
创建数据库表:在数据库中创建一个表,其中包含一个
BLOB
类型的字段,用于存储图片的字节数组。 -
插入数据:将转换后的字节数组插入到数据库的
BLOB
字段中。 -
读取数据:从数据库中读取字节数组,并将其转换回图片格式进行显示。
示例代码片段如下:
// 假设已经有一个数据库连接对象 `db`
const tableName = 'image_table';
const columnName = 'image_data';
// 创建表
db.executeSql(`CREATE TABLE IF NOT EXISTS ${tableName} (id INTEGER PRIMARY KEY AUTOINCREMENT, ${columnName} BLOB)`);
// 将图片转换为字节数组
const imageSource = image.ImageSource.createFromPixelMap(pixelMap);
const imageBytes = imageSource.getBytes();
// 插入数据
db.executeSql(`INSERT INTO ${tableName} (${columnName}) VALUES (?)`, [imageBytes]);
// 读取数据
db.executeSql(`SELECT ${columnName} FROM ${tableName} WHERE id = ?`, [id], (resultSet) => {
const imageBytes = resultSet.rows.item(0)[columnName];
const imageSource = image.ImageSource.createFromBytes(imageBytes);
// 显示图片
});
通过这种方式,可以直接将图片数据存储在数据库中,而不需要存储图片的地址。
在HarmonyOS鸿蒙Next中,若要将图片直接存入数据库而非存储地址,可以将图片转换为字节数组(byte[])后存储。首先,使用Image
类加载图片,然后通过ImageSource
将其转换为PixelMap
,再使用PixelMap
的getPixelBytes
方法获取字节数组。最后,将字节数组存入数据库的BLOB类型字段中。读取时,将字节数组转换回PixelMap
并显示。这种方式适合小图片,大图片建议仍使用文件存储并保存路径。