npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-save-base-sixty-four

v0.1.3

Published

Allows you to save a base64-encoded image to your Photos or share it via the native share sheet.

Downloads

1

Readme

react-native-save-base-sixty-four

Allows you to save a base64-encoded image to your Photos or share it via the native share sheet.

About this module

This module was developed specifically for working with base64 encoded images, with an emphasis on Android support.

If you are only looking for share sheet functionality, I would recommend using react-native-share. It supports base64 strings out of the box with no additional work.

If you are looking for the ability to save a base64 string to the camera roll, specifically with support for the current version of Android as well as legacy versions of Android, this module will help you with that.

Android support

This module provides support for:

  • Scoped storage in Android Q
  • Legacy storage in versions of Android older than Q

Please also note the permission checking in the example app as requesting and verifying permissions differs between Q and older versions of Android.

When a user does not accept permissions, a few things happen:

  • On Android Q and above:
    • The user can share via the share sheet
    • Saving to device is successful
  • On older versions of Android:
    • Sharing via the share sheet crashes due to lack of permission
    • Saving to device crashes due to lack of permission

For the above reasons, it's recommended to check for permissions on Android before performing any operations that require them, specifically on older versions of Android. See the Managing Permissions section below for an example.

iOS support

When a user does not accept permissions:

  • When attempting to share via the share sheet:
    • Sharing is successful (no permissions required for this action)
  • When attempting to save:
    • The promise will resolve with a value of false so you can handle the failure as expected due to the user not granting permission

Installation

npm install react-native-save-base-sixty-four --save
yarn add react-native-save-base-sixty-four

iOS

Add this to your Info.plist:

<key>NSPhotoLibraryAddUsageDescription</key>
<string>Save photo to device</string>

The <string> value above can be anything you like—this will be the message that shows up when the user is prompted to allow photo access.

Android

Add this to your AndroidManifest.xml:

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

Usage

Call either the share or save method with the base64 encoded string and optional options for Android.

share(base64ImageString: string, options: SaveBase64ImageOptions)

import SaveBase64Image from 'react-native-save-base-sixty-four';

// Native share sheet
//    with async await
try {
  const success = await SaveBase64Image.share(base64ImageString, options);
  if (!success) {
    // 😭 user did not grant permission
  }
} catch (error) {
  // 💥 there was a crash
}

//    with promises
SaveBase64Image
  .share(base64ImageString, options)
  .then((success) => {
    if (!success) {
      // 😭 user did not grant permission
    }
  })
  .catch((error) => {
    // 💥 there was a crash
  });

save(base64ImageString: string, options: SaveBase64ImageOptions)

import SaveBase64Image from 'react-native-save-base-sixty-four';

// Save to device
//    with async await
try {
  const success = await SaveBase64Image.save(base64ImageString, options);
  if (!success) {
    // 😭 user did not grant permission
  }
} catch (error) {
  // 💥 there was a crash
}

//    with promises
SaveBase64Image
  .save(base64ImageString, options)
  .then((success) => {
    if (!success) {
      // 😭 user did not grant permission
    }
  })
  .catch((error) => {
    // 💥 there was a crash
  });

base64ImageString: string

The base64 encoded string should not have the header, i.e. data:image/png;base64, (or similar) should be removed.

options: SaveBase64ImageOptions

The options below are used in the Android module:

interface SaveBase64ImageOptions {
  mimeType?: 'image/png' | 'image/jpg';
  quality?: number;
  fileName?: string;
}
  • mimeType (default: 'image/png'): Android configuration of mime type for bitmap compression and for the share intent. Currently only supporting 'image/png' | 'image/jpg'
  • quality (default: 100): Bitmap compression quality on Android
  • fileName (default: 'image'): The filename for Android. This will be visible in the Photos app when saving and as the file name when sharing.

Managing Permissions

Here's an example implementation that uses the permissions code in the example app:

const handleBase64Image = async (base64ImageString, fileName) => {
  const imageWithoutHeader = base64ImageString.replace('data:image/png;base64,', '');

  if (Platform.OS === 'android') {
    const hasPermission = await hasStoragePermissions();
    if (!hasPermission) {
      Alert.alert('Fail', 'You need to give the app permission');
      return;
    }
  }

  const success = await SaveBase64Image.save(imageWithoutHeader, { fileName });
  Alert.alert(success ? '😄 Success' : '😞 Fail');
};

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT