Search code examples
androidandroid-image

Image handling between modules of the application


I am developing an Android application where the user will take a picture and then this picture will be for example edited. My question is, what is the best practice to handle images and transfer them between different modules of the application (e.g. I've got a class which takes the photo and I want this photo to be used by another class)? The simplest options I come up with are to save to photo and then read it (I don't like this because at the end I also need to delete them and I think it is not a good practice), or having an object which will contain a bitmap of the image and then send that object (I think the disadvantage of this is the fact that this object is going to be very heavy).

I would appreciate it if you could suggest any better solutions.


Solution

  • As suggested from @karandeepsingh you can try creating a BitmapHolder that looks like this:

    //Singleton
    public class BitmapHolder {
    
    private static BitmapHolder instance = null;
    
    private List<Bitmap> photos;
    
    public  List<Bitmap> getPhotos()
    {
        return photos;
    }
    
    public void setPhoto(Bitmap photo)
    {
        this.photos.add(photo);
    }
    
    private BitmapHolder()
    {
        photos = new ArrayList<>();
    }
    
    public static BitmapHolder getInstance()
    {
        if (instance == null)
            instance = new BitmapHolder();
    
        return instance;
    }
    }
    

    And then you can add photos in the first class like:

    BitmapHolder bmpHolder = BitmapHolder.getInstance();
    bmpHolder.addPhoto((Bitmap) yourimage);
    

    And get the photos in the other class like:

    BitmapHolder bmpHolder = BitmapHolder.getInstance();
    List<Bitmap> photos = bmpHolder.getPhotos();