HarmonyOS鸿蒙Next中从文件夹获取的图片怎么查看他的图片大小(比如10M,50KB这种)
HarmonyOS鸿蒙Next中从文件夹获取的图片怎么查看他的图片大小(比如10M,50KB这种) 从文件夹中获取的图片。获取到的图片有宽高等信息,但是没有图片大小的信息。这种情况下我怎么得到图片的大小呢。
resourceMgr.getRawFileContent('test.jpg').then((fileData : Uint8Array) => {
console.log("Succeeded in getting RawFileContent")
// 获取图片的ArrayBuffer
const buffer = fileData.buffer.slice(0);
}).catch((err : BusinessError) => {
console.error("Failed to get RawFileContent")
});
请问怎么获取
更多关于HarmonyOS鸿蒙Next中从文件夹获取的图片怎么查看他的图片大小(比如10M,50KB这种)的实战教程也可以访问 https://www.itying.com/category-93-b0.html
参考下这个demo,用fs读取文件大小,按1000换算可以拿到图片文件的大小:
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';
import picker from '@ohos.file.picker';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
import buffer from '@ohos.buffer';
async function get() {
const photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; // 过滤选择媒体文件类型为IMAGE
photoSelectOptions.maxSelectNumber = 5; // 选择媒体文件的最大数目
let uris: Array<string> = [];
const photoViewPicker = new photoAccessHelper.PhotoViewPicker();
photoViewPicker.select(photoSelectOptions).then((photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
uris = photoSelectResult.photoUris;
console.info('photoViewPicker.select to file succeed and uris are:' + uris);
let file = fs.openSync(uris[0], fs.OpenMode.READ_WRITE);
fs.stat(file.fd).then((stat: fs.Stat) => {
console.info("lstat succeed, the size of file is " + stat.size);
}).catch((err: BusinessError) => {
console.error("lstat failed with error message: " + err.message + ", error code: " + err.code);
});
}).catch((err: BusinessError) => {
console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
}
@Entry
@Component
export struct Index {
build() {
Column() {
Button('选择并保存').onClick(() => {
get()
})
}
}
}
更多关于HarmonyOS鸿蒙Next中从文件夹获取的图片怎么查看他的图片大小(比如10M,50KB这种)的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,获取文件夹中图片的大小可以通过使用File类来实现。首先,你需要通过File类获取图片文件的路径,然后使用File类的length()方法来获取文件的大小。length()方法返回的是文件的大小,单位是字节(bytes)。你可以将这个值转换为更常见的单位,如KB或MB。
以下是一个简单的示例代码:
import fileio from '@ohos.fileio';
// 假设你有图片文件的路径
let filePath = 'path/to/your/image.jpg';
// 使用fileio.statSync获取文件信息
let fileStat = fileio.statSync(filePath);
// 获取文件大小(单位为字节)
let fileSizeInBytes = fileStat.size;
// 转换为KB或MB
let fileSizeInKB = fileSizeInBytes / 1024;
let fileSizeInMB = fileSizeInKB / 1024;
// 输出文件大小
console.log(\`File size: \${fileSizeInBytes} bytes, \${fileSizeInKB.toFixed(2)} KB, \${fileSizeInMB.toFixed(2)} MB\`);
这段代码通过fileio.statSync获取文件的状态信息,然后从状态信息中提取文件的大小。你可以根据需要将文件大小转换为不同的单位并输出。
在HarmonyOS鸿蒙Next中,获取文件夹中的图片大小,可以通过以下步骤实现:
- 使用File类:首先,通过
File类获取图片文件。 - 获取文件长度:调用
File的length()方法,获取文件的字节大小。 - 转换单位:将字节大小转换为更易读的单位(如KB、MB)。
示例代码:
File file = new File("路径/图片名称.jpg");
long fileSize = file.length(); // 获取字节大小
String sizeInKB = fileSize / 1024 + " KB"; // 转换为KB
String sizeInMB = fileSize / (1024 * 1024) + " MB"; // 转换为MB
这样即可获取并显示图片的大小信息。

