如何使用 expo 从文件系统中读取 jpg 文件? [英] How to read a jpg file from file system using expo?

查看:19
本文介绍了如何使用 expo 从文件系统中读取 jpg 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用代码使用 expo 下载了一张图片(a.jpg):

I downloaded an image(a.jpg) using expo using the code:

 FileSystem.downloadAsync(
                    httpUrl,
                    FileSystem.documentDirectory + location
                ).then((result)=>{
                    const uri = result.uri;
                }).catch((err)=>{
                    console.log("​getFile -> err", err);}
);

文件已成功保存在文件系统中.后来当我尝试读取文件时,我收到一个错误,即无法读取文件.用于读取文件的代码:

The file is successfully saved in the file system. Later when I try to read the file, I get an error that file couldn't be read. Code used to read the file:

const fileInfo = await FileSystem.getInfoAsync(uri);
if(fileInfo.exists){
            FileSystem.readAsStringAsync(uri).then(data => {
                const base64 = 'data:image/jpg;base64' + data;
                resolve(data) ; 
            }).catch(err => {
                console.log("​getFile -> err", err);
                reject(err) ;
            });
 }

以上代码返回文件无法读取的错误.fileInfo.exists 为真,因为文件存在于文件系统中.

The above code returns an error that file couldn't be read. fileInfo.exists is true as the file exists in the file system.

 ​getFile -> fileInfo Object {
    "exists": 1,
     "isDirectory": false,
     "modificationTime": 1547272322.8714085,
     "size": 51725,
    "uri": "file:///Users/deeparora/Library/Developer/CoreSimulator/Devices/A2DC4519-       C18C-4512-8C23-E624A1DAA506/data/Containers/Data/Application/6D7B23AA-      A555-4F9A-B9D1-EB5B9443CCB6/Documents/ExponentExperienceData/       %2540anonymous%252Fhola-vet-6faee8ac-e309-4d5b-a1c0-6f8688f8a508/a.jpg",
:}

读取文件时出错:

err [Error: File 'file:///Users/deeparora/Library/Developer/CoreSimulator/Devices/A2DC4519-C18C-4512-8C23-E624A1DAA506/data/Containers/Data/
Application/6D7B23AA-A555-4F9A-B9D1-EB5B9443CCB6/
Documents/ExponentExperienceData/%2540anonymous%252Fhola-vet-6faee8ac-e309-4d5b-a1c0-6f8688f8a508/a.jpg' could not be read.]

如果我尝试读取文本文件(a.json)而不是 jpg(a.jpg),则一切正常.因此, FileSystem.readAsStringAsync 适用于文本文件,而不适用于 jpg.可能需要提供其他参数作为此方法的选项,以便将 jpg 读取为 base64 字符串.

If instead of jpg(a.jpg), I try to read a text file(a.json), everything works well. So, FileSystem.readAsStringAsync works fine for the text file, not for jpg. May be, there is need to provide other parameters as options to this methods for it to read jpg as base64 string.

推荐答案

这是因为你没有告诉FileSystem.readAsStringAsync你需要的编码类型是base64.

It's due to the fact that you are not telling FileSystem.readAsStringAsync that the encoding type that you require is base64.

尝试使用

let options = { encoding: FileSystem.EncodingTypes.Base64 };
FileSystem.readAsStringAsync(uri, options).then(data => {
            const base64 = 'data:image/jpg;base64' + data;
            resolve(base64); // are you sure you want to resolve the data and not the base64 string? 
        }).catch(err => {
            console.log("​getFile -> err", err);
            reject(err) ;
        });

您可以在文档中查看有关不同选项的更多信息.https://docs.expo.io/versions/latest/sdk/filesystem#expofilesystemreadasstringasyncfileuri-options

You can see more about the different options in the docs. https://docs.expo.io/versions/latest/sdk/filesystem#expofilesystemreadasstringasyncfileuri-options

这里是使用 async/await https://小吃.expo.io/Hk-m38wfN

这篇关于如何使用 expo 从文件系统中读取 jpg 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆