通过whatsapp(React Native)共享消息和图像

问题描述 投票:0回答:1

下午好。我正在尝试与文本共享图像,但该图像未共享,共享时仅显示文本。我根据其他主题编写了此代码,该图像正在转换为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);
           });
   }
react-native firebase-realtime-database
1个回答
0
投票

也许对您有帮助,在这里最重要的部分(以及对我解决问题有帮助的部分是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)
© www.soinside.com 2019 - 2024. All rights reserved.