react-native-fbsdk - 在android上分享照片错误

时间:2018-01-15 16:01:07

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

当我尝试通过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",

代码段。

转换为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();
    })
}

1 个答案:

答案 0 :(得分:1)

不幸的是,react-native-fbsdk暂时不支持Base64数据。

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

实际上可以处理over herebuildSharePhoto方法用于将即将到来的JS数据转换为SharePhoto对象。