I am using the Ion library, and its working well. But on the other Hand I have to download image and save it to app directory.
What I am trying to do is , using a Image view in the navigation view header and want to download image one time and to show that image for the next time until and Unless I discharge it
So I as I am using the Ion library to make calls to server , SO I am thinking to Why not use Ion library for this purpose too. All I know that it has a valid function for downloading image and to show it in image view like that
// This is the "long" way to do build an ImageView request... it allows you to set headers, etc.
Ion.with(context)
.load("http://example.com/image.png")
.withBitmap()
.placeholder(R.drawable.placeholder_image)
.error(R.drawable.error_image)
.animateLoad(spinAnimation)
.animateIn(fadeInAnimation)
.intoImageView(imageView);
// but for brevity, use the ImageView specific builder...
Ion.with(imageView)
.placeholder(R.drawable.placeholder_image)
.error(R.drawable.error_image)
.animateLoad(spinAnimation)
.animateIn(fadeInAnimation)
.load("http://example.com/image.png");
I really do not know how to use it and save the image in the app directory rather then showing it directly in the Image view.
I want to download image and save image in the app directory. Can I use Ion library for this purpose or I have to do something more. ?? Please help.
Ion.with(this).load("url").withBitmap().asBitmap()
.setCallback(new FutureCallback<Bitmap>() {
@Override
public void onCompleted(Exception e, Bitmap result) {
// do something with your bitmap
}
});
You will get a bitmap on the onCompleted callback, that is your image as bitmap. Now you can save easily where you want.
For save bitmap :
//the string here is the file name
public static void saveBitmap(Context context, Bitmap original, String name) {
try {
File imageFile = createPngFileFromBitmap(name, original);
addImageToGallery(imageFile.getAbsolutePath(), context);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void addImageToGallery(final String filePath, final Context context) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
values.put(MediaStore.MediaColumns.DATA, filePath);
context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
public static File createPngFileFromBitmap(String name, Bitmap bitmap) throws FileNotFoundException {
File picture = createPngFile(name);
OutputStream stream = new FileOutputStream(picture);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return picture;
}
private static File createPngFile(String name) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Your folder name");
if (!file.mkdirs()) {
} else {
file.mkdirs();
}
return new File(file.getAbsolutePath(), name);
}
Good luck!