0

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.

Allay Khalil
  • 674
  • 3
  • 11
  • 31

2 Answers2

1
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!

Robert Banyai
  • 1,329
  • 12
  • 14
0

Yes, you can save your image to your internal/external memory with Ion. You can refer to their official documentation for more detail

Ion.with(context)
.load("http://example.com/really-big-file.zip")
// have a ProgressBar get updated automatically with the percent
.progressBar(progressBar)
// and a ProgressDialog
.progressDialog(progressDialog)
// can also use a custom callback
.progress(new ProgressCallback() {@Override
   public void onProgress(int downloaded, int total) {
       System.out.println("" + downloaded + " / " + total);
   }
})
.write(new File("/sdcard/really-big-file.zip"))
.setCallback(new FutureCallback<File>() {
   @Override
    public void onCompleted(Exception e, File file) {
        // download done...
        // do stuff with the File or error
    }
});
Emin Ayar
  • 1,104
  • 9
  • 13