react-native - 通过whatsapp(React Native)分享消息和图像
问题描述
下午好。我正在尝试与文本共享图像,但图像未共享,共享时仅显示文本。我根据其他主题制作了这段代码,图像正在转换为base64,但仍然没有出现。注意:URI 是存储在 firebase 中的图像。
onSharePress() {
const fs = RNFetchBlob.fs;
let imagePath = null;
RNFetchBlob.config({
fileCache: true
})
.fetch("GET", "https://i.kinja-img.com/gawker-media/image/upload/s--HqfzgkTd--/c_scale,f_auto,fl_progressive,q_80,w_800/wp2qinp6fu0d8guhex9v.jpg")
.then(resp => {
imagePath = resp.path();
return resp.readFile("base64");
})
.then(async base64Data => {
var base64Data = `data:image/png;base64,` + base64Data;
// here's base64 encoded image
console.log(base64Data);
const shareOptions = {
title: 'Title',
message: 'Message to share',
url: base64Data,
subject: 'Subject'
};
alert(base64Data);
await Share.share(shareOptions);
return fs.unlink(imagePath);
});
}
解决方案
也许它对您有帮助,这里最重要的部分(以及帮助我解决问题的方法)是encodeURI
,请关注我的代码:
import Share from 'react-native-share'
import RNFetchBlob from 'rn-fetch-blob'
//
const base64File = //base64 file
const pdf = "data:application/pdf;base64," + base64File
let filePath = '';
const path = RNFetchBlob.fs.dirs.DocumentDir + '/someFileName.pdf';
const configOptions = { fileCache: true, path };
let fullPath = await RNFetchBlob.config(configOptions).fetch('GET', encodeURI(fileUrl))
filePath = fullPath.path();
let options = {
title: 'Share via'
type: type,
url: filePath // (Platform.OS === 'android' ? 'file://' + filePath)
};
Share.open(options)
推荐阅读
- java - 使用 android 中的改造在数组中发布数据
- java - 无法通过改造 2 获得响应消息
- python - 如何转换熊猫数据框
- ios - RxSwift - 如何具有可读的 combineLatest 事件元素变量名称?
- c# - 如何在 QRCoder C# 库中解码 qr 文件的文本?
- postgresql - 为什么磁盘空间问题会破坏 pg_statistic 表
- python - python 2.7不向nginx发送主机头
- r - 将“*”移动到 R 中的新列
- javascript - 在html5画布的矩形内对齐文本
- python-3.x - np.percentile 似乎没有给出正确的输出