DEV Community

Kiran Lakhotia
Kiran Lakhotia

Posted on

Sharing GIF via Intent from React Native

I had a GIF, loaded via a URL (the image is stored in a S3 bucket), that I wanted to be sharable from my (react-native) app, via WhatsApp for example.

At first I thought I could simply use the URL in the Intent call to share the GIF, but that didn't work. Turns out you have to download and save the image locally. Then you can share it via an Intent.

Our app uses the Fresco library, so the below code is based on that. The first step was to get a reference to the image pipeline and build an image request to download the GIF.

//final Activity activity = ...;
//final Context context = ...;
//final String message = ...;
//final String imageUrl = ...;
//final String tempFileName = ...;

ImagePipeline imagePipeline = Fresco.getImagePipeline();
ImageRequest imageRequest = ImageRequestBuilder

Next we start downloading the GIF and wait until the download is complete (you can also do this asynchronously).

DataSource<CloseableReference<PooledByteBuffer>> dataSource = imagePipeline.fetchEncodedImage(imageRequest, context);

CloseableReference<PooledByteBuffer> result = DataSources.waitForFinalResult(dataSource);

Assuming the GIF was downloaded OK, I had to save the image on the local file system and then create a URI to that file so it can be used in the share Intent.

if (result != null) {
   final PooledByteBuffer buffer = result.get();
   if (buffer != null) {
      final byte[] data = new byte[buffer.size()];, data, 0, data.length);

      final File localFile = new File(context.getExternalFilesDir(null), tempFileName);

      try(OutputStream os = new FileOutputStream(localFile)) {
      } catch(Exception e) {
         //handle error...

      Uri contentUri = FileProvider.getUriForFile(activity, context.getPackageName() + ".provider", localFile);

      Intent intent = new Intent();
      Intent actionIntent = intent;
      actionIntent = Intent.createChooser(intent, "Select an app to share");
      //Add text and then Image URI
      intent.putExtra(Intent.EXTRA_TEXT, message);
      intent.putExtra(Intent.EXTRA_STREAM, contentUri);

And that's it. The above code requires (at least) the following permissions

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

Top comments (1)

kris profile image

Exactly the article I have been looking for. Thank you for this. The integration information is on point. The use of GIF in React Native and sharing it with intent using the Fresco library. Simple and easy to understand.