Fast and Efficient way to transfer images between Activites in Android

By | July 14, 2017

We often need to transfer images between activities.

Well there are many methods to transfer images.

  1. You can make the image into a Base64 String and pass it as String argument extra in a bundle between activities
  2. You can save it to a disc and read it in the other activity.

Well, these methods will always work, but what could be the fastest and efficient solution.

Here is it. Just make a singleton class and set the image in a variable in it.

How to Do?

I am having a singleton class named “BitmapSingleton” which has a image variable with a setter and getter method.

Singleton Class

public class BitmapSingleton {

    private Bitmap image = null;

    private static final BitmapSingleton instance = new BitmapSingleton();

    public static BitmapSingleton getInstance() {
        return instance;
    }

    private BitmapSingleton() {
    }

    public Bitmap getBitmap() {
        return image;
    }

    public void setBitmap(Bitmap image) {
        this.image = image;
    }

}

For sending images between activities…

From activity A -> BitmapSingleton.getInstance().setBitmap(bitmap);

From activity B -> BitmapSingleton.getInstance().getBitmap(bitmap);

After you use it, you should do BitmapSingleton.getInstance().setBitmap(null); in activity B.

All done.

Please leave your valuable comments below this post.

Leave a Reply

Your email address will not be published.