Silverfall05
Silverfall05

Reputation: 1127

React Native sharing image to Instagram from DocumentDirectoryPath

I have made custom native module for React Native that's need to share images from path /data/user/0/com.instagramscheduler/files/downloaded.jpg (that is my application) and to be clear file exists in that folder.

But the problem is that it is not sharing, when I share to Instagram it says that it is unable to load file. I have checked and uri.toString() == "file:///data/user/0/com.instagramscheduler/files/downloaded.jpg"

My React Native custom share method:

public void share() {
    // Create the new Intent using the 'Send' action.
    Intent share = new Intent(Intent.ACTION_SEND);

    // Set the MIME type
    share.setType("image/jpeg");
    share.setPackage("com.instagram.android");

    // Create the URI from the media
    File media = new File("/data/user/0/com.instagramscheduler/files/downloaded.jpg");
    Uri uri = Uri.fromFile(media);

    // Add the URI to the Intent.
    share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    share.putExtra(Intent.EXTRA_STREAM, uri);

    // Broadcast the Intent.
    activity.startActivity(Intent.createChooser(share, "Share To"));
}

Here is my manifest if it helps:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.instagramscheduler">

<uses-permission android:name="android.permission.INTERNET" />

<application
  android:allowBackup="true"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:theme="@style/AppTheme">
  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>

Here is logcat if needed (starting from when share dialog open till it fails to share on Instagram): https://gist.github.com/Azael05x/f706925668ce9b8dd9970a81ed23e979

Upvotes: 0

Views: 1836

Answers (1)

Silverfall05
Silverfall05

Reputation: 1127

  1. As I was downloading and saving images instead of saving image to your application folder, save it to public folder. And it worked, because Instagram did not have access to my apps private folders.
  2. Then I checked on other phone and it did not work there. Reason was that Instagram was with disabled storage permission.

Upvotes: 1

Related Questions