Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

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

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);
        }
    }
}
question from:https://stackoverflow.com/questions/65858093/display-image-in-imageview-and-save-it-to-internal-storage-on-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...