Display Image in ImageView and save it to internal storage on Android

285 Views Asked by At

I am displaying some images in recyclerview using the picasso library from firebase storage, what I want to achieve is to save the image into app-specific internal storage as soon as the image is displayed in the imageview.

Then when I open the app next time I want to fetch the image from internal storage, if the image is not available there then fetch it from firebase urls. I'm able to show the images using Picasso library but then I'm stuck, I just begun android development and don't know how to proceed from here. Below is my code.

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
    private List<LoadImage> loadImages;
    private Map<String, Bitmap> mBitmaps = new HashMap<>();

    public ImageAdapter(List<LoadImage> loadImages) {
        this.loadImages = loadImages;
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_view, parent, false);
        return new ImageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
        LoadImage currentImage = loadImages.get(position);

        Picasso.get()
                .load(currentImage.getImageUrl())
                .fit()
                .into(holder.imageViewDownload);
    }

    @Override
    public int getItemCount() {
        return loadImages.size();
    }

    public class ImageViewHolder extends RecyclerView.ViewHolder {

        ImageView imageViewDownload;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);

            imageViewDownload = itemView.findViewById(R.id.image_view_download);
        }
    }
}
1

There are 1 best solutions below

1
On

You don't need to save images to internal storage. Picasso library does this for you when you load image it caches the image to app-specific directory. Next time it will load cached image if available if not it will download image from url and cache