Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Android – Download and save images to SD card using Picasso

In my blog I always try to address pain points of developers, so does most of the naive developers face difficulty to download and save images for their respective Android apps into SD card. And they ended up writing more complicated logic and which may create other issues, for that reason only I am going to explain a simple and robust way to downloading and saving images in SD card using Picasso.

Picasso is a famous library from Square, used by developers for downloading images and displaying them but many do not know that it has capability of not only downloading but also saving those images as per individual’s requirement, let’s have a look on how one can achieve this.

Include Picasso into Gradle

First of all we need to include the library to the build.gradle file of the App module.

dependencies {
    //other libraries
    compile "com.squareup.picasso:picasso:2.4.0"
}

Use of Target

Picasso has an Interface called Target, you just need to create another Class which implements this Interface and you can save the downloaded image bitmap to a file of your required location in SD Card. Here is a sample class for your reference. Once your class is ready you just need to call while downloading images.

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import java.io.File;
import java.io.FileOutputStream;

public class PhotoLoader implements Target {
    private final String name;
    private ImageView imageView;

    public PhotoLoader(String name, ImageView imageView) {
        this.name = name;
        this.imageView = imageView;
    }

    @Override
    public void onPrepareLoad(Drawable arg0) {
    }

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) {
        File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + name);
        try {
            file.createNewFile();
            FileOutputStream ostream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 75, ostream);
            ostream.close();

            imageView.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onBitmapFailed(Drawable arg0) {
    }
}

Read about other libraries from Square –

  • How to use OkHttp3 in Android
  • Phrase – Android string formatting library

 Downloading images

Now to download the image to display you need to call Picasso class which actually downloads the images –

String url = "https://www.w3schools.com/css/trolltunga.jpg";
String imageName = "trolltunga.jpg";
Picasso.with(mContext)
                    .load(url)
                    .into(new PhotoLoader(imageName , mImageView));

As you can see I am using PhotoLoader class which implements Target Interface.

So it is pretty easy to download and save images in SD card using Picasso, and another good part of using it is a very small library compare to other image downloading libraries available.

I hope this article will be helpful for you, so do not forget to subscribe and if you are already subscribed then share this post to you friends and colleagues so they can also take advantage.

Keep coding!!!

The post Android – Download and save images to SD card using Picasso appeared first on { OneTouchCode.com }.



This post first appeared on Onetouchcode, please read the originial post: here

Share the post

Android – Download and save images to SD card using Picasso

×

Subscribe to Onetouchcode

Get updates delivered right to your inbox!

Thank you for your subscription

×