react-native-share在Android 7及更高版本中无法正常工作?

时间:2018-04-12 11:08:38

标签: android react-native share

我正在使用我的应用中的react-native-share。共享选项工作正常,直到Android版本5.我不确定它在版本6中正常工作。

但我确信分享在版本7及更高版本中不起作用。会是什么问题。我吃了一整天。我试图更改版本

我的共享代码是

_downloadImageAndShare(url ,title, message) {
    this.setState({loading: true})
    RNFetchBlob.config({ fileCache: true })
      .fetch('GET', url)
      .then(resp => resp.readFile('base64')
          .then(base64 => ({ resp, base64 })))
      .then(obj => {
        const headers = obj.resp.respInfo.headers;
        const type = headers['Content-Type'];
        const dataUrl = 'data:' + type + ';base64,' + obj.base64;
        return { url: dataUrl, title, message };
      })
      .then(options => {
        Share.open(options)
        this.setState({loading: false})
      });        
   }

以及

<uses-sdk
            android:minSdkVersion="16"
            android:targetSdkVersion="26" /> 



android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'

    defaultConfig {
        applicationId "com.uplode"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }

但它没有成功。确切的问题是(在android 7版本中):**

1. whatsapp not even opening while trigger share button.
2. Hangouts only allows to share the url and message, not allowing to share image.
3. E-mail also the same scenario of hangouts

**我现在该怎么办?提前谢谢

1 个答案:

答案 0 :(得分:2)

这似乎是API 23及更高版本的权限问题。请尝试以下快速修复方法。

import { PermissionsAndroid } from 'react-native';

...

async _downloadImageAndShare(url ,title, message) {
this.setState({loading: true})

const granted = await PermissionsAndroid.check(
  'android.permission.WRITE_EXTERNAL_STORAGE'
 );
 if (!granted) {
  const response = await PermissionsAndroid.request(
    'android.permission.WRITE_EXTERNAL_STORAGE'
  );
  if (!response) {
    return;
  }
 }

RNFetchBlob.config({ fileCache: true })
  .fetch('GET', url)
  .then(resp => resp.readFile('base64')
      .then(base64 => ({ resp, base64 })))
  .then(obj => {
    const headers = obj.resp.respInfo.headers;
    const type = headers['Content-Type'];
    const dataUrl = 'data:' + type + ';base64,' + obj.base64;
    return { url: dataUrl, title, message };
  })
  .then(options => {
    Share.open(options)
    this.setState({loading: false})
  });        

}

此修补程序将在尝试将base64图像共享到WhatsApp之前检查所需的权限是否处于活动状态。您可以read more on the issue here