Download Images in Android from URL: Part 2

This article assumes that you have already gone through the tutorial that explains how to add VutaImage library to your project. If not Click Here

Single Image Download

  1. Supplying only the Url/ Image source

You can use VutaImage with only the image url and the library will resolve the filename from the supplied link.

Example:

Downloading an image from www.example.com/images/image1.png, the library will resolve the filename of the image to image1.png and this is what will be saved in your preferred storage location.

NB: In this case, we assume that the defaultStorage location has been set otherwise we fallback to saving in sdcard root directory.

To set the default storage for your assets, use this method


VutaImage.setDefaultStorageDir("/my/dir/");

This is the code sample that will allow you to download an image from the given url

final String imageUrl = "http://www.example.com/projects/portrait-o.jpg";
VutaImage.download(imageUrl, new ImageDownloadCallback() {
 @Override
 public void progress(int elapsed, int totalSize) {
    // TODO Auto-generated method stub
 }

 @Override
 public void done(boolean success) {
    // TODO Auto-generated method stub
 }
});
  1. Supplying both the image url and the filename
final String filename = VutaImage.getExternalStorage()+"/filename.jpg";

VutaImage.download(imageUrl, filename, new ImageDownloadCallback() {
 @Override
 public void progress(int elapsed, int total) {
     // TODO Auto-generated method stub
 }
 @Override
 public void done(boolean success) {
    // TODO Auto-generated method stub
 }
});
  1. Using VutaImageItem object which will contain the image url and the filename

You can use an instance of VutaImageItem to download an Image.

String sdCard = VutaImage.getExternalStorage( );
VutaImageItem vutaImageItem = new VutaImageItem(
	"http://domain.com/img.png", sdcard+"/image.png" );

VutaImage.download( vutaImageItem, new ImageDownloadCallback() {
   @Override
   public void progress(int elapsedSize, int totalSize) {
       // TODO Auto-generated method stub
   }

   @Override
   public void done(boolean success) {
     // TODO Auto-generated method stub
   }
});

Leave a Reply

Your email address will not be published. Required fields are marked *