khalifathul
khalifathul

Reputation: 668

react-native-share not working properly in android version 7 and more?

I am working on react-native-share in my app. The share options are working fine till the android version 5. I am not sure its working fine in version 6.

But I am sure share is not working in version 7 and above. What will be the issue. I ate my entire day. I have tried to change the versions

my share code is

_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})
      });        
   }

and also

<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"
        }
    }

But it didn't get work. The exact issues are (in android 7 version): **

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

** what should i do now? thanks in advance

Upvotes: 2

Views: 2151

Answers (1)

Seunope
Seunope

Reputation: 4814

It appears to be a permission issue for API 23 and higher. Try this quick fix below.

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})
  });        

}

This fix will check if the needed permission is active before attempting to share base64 image to WhatsApp. You can read more on the issue here

Upvotes: 2

Related Questions