-2

I am currently developing an android app with takes image URls from my custom server and loads them into a ImageView. I have used Picasso for this. I have successfully completed the downloading process but how I want to save the image in the device so that the user can view them even when the app is run offline without loss in quality of image. I plan to store the image location into my database. I just need the code to save it to memory and the way to get access to that image in another class. There are exactly 5 images.

Sagar Acharya
  • 1,763
  • 2
  • 19
  • 38

1 Answers1

1

You are loading images from picasso , try this

@Override
            public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {

                        File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + url);
                        try {
                            file.createNewFile();
                            FileOutputStream ostream = new FileOutputStream(file);
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
                            ostream.flush();
                            ostream.close();
                        } catch (IOException e) {
                            Log.e("IOException", e.getLocalizedMessage());
                        }
                    }
                }).start();

            }

It will save the loaded file in device storage. Make sure to have write_storage permission in manifest.

Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26