【问题标题】:react-native-fbsdk - Share photo error on androidreact-native-fbsdk - 在 android 上分享照片错误
【发布时间】:2018-01-15 16:01:06
【问题描述】:

当我尝试通过 Facebook ShareDialog 分享照片时,我收到此错误:

Facebook 共享失败并出现错误:错误:媒体方案不受支持 URI:数据

为了将图像转换为 blob,我使用 react-native-fetch-blob Facebook 分享在 iOS 上运行良好。我什至尝试用 iOS 设备上生成的数据对 dataUri 进行硬编码。

版本:

"react": "16.0.0-alpha.12",
"react-native": "0.48.0",
"react-native-fbsdk": "0.6.3",
"react-native-fetch-blob": "^0.10.8",

代码 sn-ps。

转换为dataurl:

toDataURL(url) {
    return new Promise((resolve, reject) => {
        RNFetchBlob.fetch('GET', url)
      .then((res) => {
        let base64Str = res.base64();
        base64Str = 'data:image/jpg;charset=utf-8;base64,' + base64Str;
        resolve(base64Str);
      })
      .catch((errorMessage, statusCode) => {
        reject(errorMessage);
      })
    });
}

分享:

if (attachments.length) {
  let attachmentsPromises = attachments.map(a => {
    return this.toDataURL(a.url);
  });

  Promise.all(attachmentsPromises).then(res => {
    let shareContent = {
      contentType: "photo",
      contentUrl: "http://www.google.com",
      contentDescription: text,
      quote: text,
      photos: res.map(dataUrl => {
        return {
          caption: text,
          imageUrl: dataUrl,
          userGenerated: true
        };
      })
    };

    shareWithFacebook(shareContent);
  });
} else {
  let shareContent = {
    contentType: "link",
    contentUrl: "http://www.google.com",
    quote: text
  };
  shareWithFacebook(shareContent);
}

function shareWithFacebook(shareContent) {
  console.log('sharing on facebook', shareContent);
  ShareDialog.canShow(shareContent)
    .then(function(canShow) {
      console.log('canshow', canShow);
      if (canShow) {
        return ShareDialog.show(shareContent);
      } else {
        onSuccess();
      }
    })
    .then((result) => {
      if (result && result.isCanceled) {
        alert("Facebook share cancelled");
      } 
      onSuccess();
    })
    .catch((error) => {
      alert("Facebook share fail with error: " + error);
      onSuccess();
    })
}

【问题讨论】:

    标签: android facebook react-native react-native-fbsdk


    【解决方案1】:

    很遗憾,react-native-fbsdk 暂时不支持 Base64 数据。

    https://developers.facebook.com/docs/react-native/faq#faq_631338030360975

    其实可以处理over herebuildSharePhoto 方法用于将传入的 JS 数据转换为 SharePhoto 对象。

    【讨论】:

      猜你喜欢
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      • 2018-01-11
      • 2016-08-31
      相关资源
      最近更新 更多