第三方API返回base64编码的"二维码图像"

  • CamerRoll-不支持将base64图像保存到相册
  • react 本机获取Blob-
  • react 本机fs-
  • 很少有npm模块具有非常小的Github star(<10)

React Native Fetch Blob维护人员失踪,因此没有人回答Github问题,

  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));
  });

Anyone deal with this problem before?

How to save a base64 encode Image string to User album? (as a jpg or png file)

因为我需要一个没有CORS头的API,

我必须在我的Android手机上运行它才能正常工作

我计划使用剪贴板保存base64字符串,

推荐答案

现在,您只能使用react native fetch blob来实现这一点.

只需更换RNF即可.写文件

RNFetchBlob.fs.writeFile(file_path, image_data, 'base64')

如果希望在本机OS viewer中查看文件,只需

                if (isAndroid) {
                  RNFetchBlob.android.actionViewIntent(file_path, 'application/pdf');
                } else {
                  RNFetchBlob.ios.previewDocument(file_path);
                }

React-native相关问答推荐

SignalR 与 React Native Expo:: 什么都没有发生

如何在 react-navigation v6 中更改标题高度

React Native Child Parent 通信

React Native Android:how to remove "overscroll effect" of ScrollView

在 React Native 中从 Modal 打开 Modal

ReactJS 应用多种内联样式

从 React-Native 应用程序中的另一个类访问静态变量?

为什么会出现此错误:Invariant Violation: Cannot update during an existing state transition

在根项目ReactNativeStarter中找不到任务installRelease

运行react应用程序时出错

library not found for -lRCTGeolocation

ReactNative FlatList 一次渲染所有元素?

如何判断 React Native 中的 AsyncStorage 中是否存在密钥? getItem() 总是返回一个promise对象

React Native Remote Debugger 在 Chrome 中显示缓存的包

使用带有样式组件的动画

更改按钮 colored颜色 onPress(切换功能)

React Native 元素,未生成 index.ios.js 或 index.android.js

Expo LAN 配置不适用于新的 ReactNative 元素

react-native:多色文本视图

python 3与本机兼容吗?还是我应该按照 react 文档的建议安装 python 2?