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.
Asked
Active
Viewed 903 times
-2
-
Have you tried to use the IO classes from Java? – Reporter May 23 '17 at 12:16
-
i looked at it but i was confused about acessing the image from file from another class. it would be better from me if i could just save it in the database – Sagar Acharya May 23 '17 at 12:27
1 Answers
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
-
how can i acess the file contents from another class? i mean accessing the image from other class – Sagar Acharya May 23 '17 at 12:25
-
-
Store url of that image in some global variable, and access it in other class using object – Abdul Kawee May 23 '17 at 12:28