HarmonyOS鸿蒙Next应用开发实战-JS访问本地图片

HarmonyOS鸿蒙Next应用开发实战-JS访问本地图片 选择鸿蒙api7 提供获取读取本地文件的api,但api7还未稳定,我们可以使用java api将想要的文件复制到js现在可以访问的internal://app/下,在使用js访问。

//存储照片到js可以访问的目录下
void setImaData(Uri uri, String name){
    String imgName = "/" + name + ".jpg";
    requestPermissionsFromUser(new String[]{"ohos.permission.READ_USER_STORAGE"}, 1234);
    HiLog.info(TAG, "私有目录:" + this.getFilesDir());
    HiLog.info(TAG, "缓存目录:" + this.getCacheDir());
    HiLog.info(TAG, "数据目录:" + this.getDataDir());
    HiLog.info(TAG, "外部目录:" + this.getExternalCacheDir());
    HiLog.info(TAG, "引用目录:" + this.getPreferencesDir());
    HiLog.info(TAG, "同步目录:" + this.getDistributedDir());
    HiLog.info(TAG, "应用目录:" + this.getDir("", Context.MODE_PRIVATE));
    File[] dataChildFiles = this.getDataDir().listFiles();
    for (File dataChildFile : dataChildFiles) {
        HiLog.info(TAG, "数据子目录:" + dataChildFile);
    }
    File[] externalFiles = this.getExternalMediaDirs();
    for (File externalFile : externalFiles) {
        HiLog.info(TAG, "外部Media目录:" + externalFile);
    }
    try {
        //该目录和JSUI中的internal://cache/目录是一个目录
        //File file = new File(this.getCacheDir() + "/111.png");
        File file = new File(this.getFilesDir() + imgName);
        HiLog.info(TAG, "文件路径:" + file);
        if (file.exists()) {
            HiLog.info(TAG, "文件已存在");
            setUriToService(imgName);
            return;
        }
        HiLog.info(TAG, "文件不存在");
        //定义数据能力帮助对象
        DataAbilityHelper helper = DataAbilityHelper.creator(getContext());
        //读取图片
        FileDescriptor fd = null;
        try {
            fd = helper.openFile(uri, "r");
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        ImageSource imageSource;
        imageSource = ImageSource.create(fd, null);
        // 设置图片参数
        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
        decodingOptions.desiredSize = new Size(200, 200);
        imageSource.createPixelmap(decodingOptions);
        //该种方式直接访问internal://app目录
        //FileOutputStream fos = new FileOutputStream("/data/user/0/com.example.abilitytransfertest/files//111.jpg");
        FileOutputStream fos = new FileOutputStream(this.getFilesDir() + imgName);
        ImagePacker imagePacker = ImagePacker.create();
        ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions();
        packingOptions.format = "image/jpeg";
        packingOptions.quality = 90;
        boolean result = imagePacker.initializePacking(fos, packingOptions);
        if (result) {
            result = imagePacker.addImage(imageSource.createPixelmap(decodingOptions));
            if (result) {
                long dataSize = imagePacker.finalizePacking();
                HiLog.info(TAG, "文件大小:" + dataSize);
            }
        }
        fos.flush();
        fos.close();
        if (file.exists()) {
            HiLog.info(TAG, "文件已存在");
            setUriToService(imgName);
            return;
        }
    } catch (IOException e) {
        HiLog.info(TAG, "文件保存出错:" + e.getMessage());
        e.printStackTrace();
    }
    File file = this.getFilesDir();
    File[] files = file.listFiles();
    for (File file1 : files) {
        HiLog.info(TAG, "File目录:" + file1);
    }
}

更多关于HarmonyOS鸿蒙Next应用开发实战-JS访问本地图片的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

跟楼主学一个实用新技巧

更多关于HarmonyOS鸿蒙Next应用开发实战-JS访问本地图片的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,使用JS访问本地图片可以通过@ohos.file.fs模块实现。首先,使用fs.openSync打开图片文件,然后通过fs.readSync读取文件内容,最后将读取的数据转换为Base64格式或直接使用。此外,还可以使用Image组件显示图片,通过src属性指定图片路径。确保在config.json中配置相应的文件访问权限。示例代码如下:

const fs = require('@ohos.file.fs');
let file = fs.openSync('path/to/image.jpg', fs.OpenMode.READ_ONLY);
let buffer = new ArrayBuffer(1024);
fs.readSync(file.fd, buffer);
fs.closeSync(file);
回到顶部