问题描述
第三方API返回以base64编码的"QR码图像",
我需要将该图像保存到用户的相册"中.
Third Party API return a "QR code image" in base64 encode,
I need save that image to User's Album.
- CamerRoll-不支持将base64图像保存到相册
- React-Native-Fetch-Blob- https://github.com/wkh237/react-native-fetch-blob
仍在调查中 - React-Native-fs- https://github.com/itinance/react-native-fs
我正在尝试这个 - 只有很少的Github星(
React-Native-Fetch-Blob维护者不见了,所以没人回答Github问题,来自createFile "rel ="noreferrer">反应性本机-获取-Blob文档未按预期工作(未将图像保存到相册中)
the React-Native-Fetch-Blob maintainer gone missing, so no one answering Github Issue,createFile
from React-Native-Fetch-Blob Document not working as expected(not saving image into album)
import fetch_blob from 'react-native-fetch-blob';
// json.qr variable are return from API
const fs = fetch_blob.fs
const base64 = fetch_blob.base64
const dirs = fetch_blob.fs.dirs
const file_path = dirs.DCIMDir + "/some.jpg"
const base64_img = base64.encode(json.qr)
fs.createFile(file_path, base64_img, 'base64')
.then((rep) => {
alert(JSON.stringify(rep));
})
.catch((error) => {
alert(JSON.stringify(error));
});
有人解决过这个问题吗?
如何将base64编码的图像字符串保存到用户相册?(作为jpg或png文件)
因为我 fetch
一个没有CORS标头的API,
我无法在远程调试JS
中对其进行调试Chrome会阻止该请求的发生,
Anyone deal with this problem before?
How to save a base64 encode Image string to User album? (as a jpg or png file)
because I fetch
an API with no CORS header,
I can't debug it in Debug JS Remotely
Chrome would stop that request from happening,
我必须在我的Android手机上运行它才能使其工作
(在真实电话上没有CORS控件)
I have to run that on my Android Phone to make it work
(no CORS control on real phone)
我打算使用剪贴板保存base64字符串,
并将其硬编码到我的代码中,
调试 react-native-fetch-blob
createFile
API
I am planing use Clipboard save base64 string,
and hardcode it in my code,
to debug what's wrong with react-native-fetch-blob
createFile
API
推荐答案
您现在只能使用react native fetch blob来实现此目的.
You can now use only react native fetch blob to achieve this.
只需将RNFS.writeFile替换为
Simply replace RNFS.writeFile with
RNFetchBlob.fs.writeFile(file_path, image_data, 'base64')
如果您希望在本机OS查看器中查看文件,只需输入
If you wish to view file in native OS viewer you can simply put
if (isAndroid) {
RNFetchBlob.android.actionViewIntent(file_path, 'application/pdf');
} else {
RNFetchBlob.ios.previewDocument(file_path);
}
这篇关于React Native将base64图像保存到相册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!