如何从博览会图像选择器的缓存文件中获取文件

时间:2021-02-11 07:29:30

标签: react-native expo

我使用 Expo Image Picker:https://docs.expo.io/versions/latest/sdk/imagepicker/

从图库或相机获取图像后,返回此结果:

{
  "cancelled":false,
  "height":1611,
  "width":2148,
  "uri":"file:///data/user/0/host.exp.exponent/cache/cropped1814158652.jpg"
}

但是,我得到的只是 uri,我需要文件,如何从缓存 uri 中获取文件?

我需要使用这个将该文件上传到服务器

module.exports.uploadFile = function(file) {
    return new Promise((resolve, reject) => {
        myServerApi.upload({
            file,
            fileName: file.name, 
        }, function(err, result) {
            if(err) reject(err);
            resolve(result);
        })
    })
}

1 个答案:

答案 0 :(得分:0)

从您发布的文档来看,/** gcc -std=c99 -o prog_c prog_c.c \ -pedantic -Wall -Wextra -Wconversion \ -Wc++-compat -Wwrite-strings -Wold-style-definition -Wvla \ -g -O0 -UNDEBUG -fsanitize=address,undefined **/ #include <stdio.h> void convert(int num, char *result) { const char *deg="0123456789ABCDEF"; result[0]=deg[num/16]; result[1]=deg[num%16]; result[2]='\0'; } int rgb(int r, int g, int b, char *output, size_t capacity) { if(capacity<7) { return -1; } convert(r, output+0); convert(g, output+2); convert(b, output+4); return 0; } int main(void) { char txt[20]; rgb(1, 2, 3, txt, sizeof(txt)); printf("%s\n", txt); return 0; } ImagePicker.launchImageLibraryAsync(options) 有一个名为 options 的布尔值:

<块引用>

base64 (boolean) -- 是否还包含 Base64 格式的图像数据。

因此,按照文档示例,您可以执行以下操作:

base64

let result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ImagePicker.MediaTypeOptions.All, allowsEditing: true, base64: true, //<-- boolean base64 aspect: [4, 3], quality: 1, }); 将是:

result

因此您可以将 { "cancelled":false, "height":1611, "width":2148, "uri":"file:///data/user/0/host.exp.exponent/cache/cropped1814158652.jpg", "base64": "......" //<-- base64 image } 传递给您的函数 result 来存储图像。类似的东西:

uploadFile

然后,正如文档所说,您也可以通过这种方式使用 base64 图像作为 uri:

<块引用>

如果 base64 选项为真,则包含 base64 属性,并且是所选图像的 JPEG 数据的 Base64 编码字符串。如果在前面加上“data:image/jpeg;base64”来创建数据 URI,则可以将其用作 Image 元素的源;例如:.