1

I have stored images on server, now i want to download that image from android code. I am using picasso library for loading images from server.

But i don't know how to download and save it in my SDCard (only using Picasso library or some other library). So, anyone suggest me any working code !!

i am using this code

Picasso.with(this).load("server url").fetch(new Callback() {
        @Override
        public void onSuccess() {

        }

        @Override
        public void onError() {

        }
    });
Mehul
  • 2,141
  • 1
  • 15
  • 15
  • If you want to save it to specific folder then use call back with _Target_ for picasso which will return bitmap and you can save that bitmap to specific folder – Piyush Apr 12 '17 at 13:27
  • i have used callback also, but i don't know from where i can get image. – Mehul Apr 12 '17 at 13:30
  • 1
    In _onBitmapLoaded_ method you can get bitmap which will be your image. – Piyush Apr 12 '17 at 13:31
  • Thanks for helping. I have used Target like Picasso.with(this).load("server url").into(myTarget); – Mehul Apr 12 '17 at 13:35

1 Answers1

0
    //save image
    public static void imageDownload(Context ctx, String url){
        Picasso.with(ctx)
                .load("http://blog.concretesolutions.com.br/wp-content/uploads/2015/04/Android1.png")
                .into(getTarget(url));
    }

    //target to save
    private static Target getTarget(final String url){
        Target target = new Target(){

            @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();

            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

Use above code for saving the bitmap in your External storage ..Please make sure your dynamic permissions and Manifest file have been set up properly.Please follow this discussion Saving image from url using Picasso?

Community
  • 1
  • 1
DRY Believer
  • 1,001
  • 11
  • 20
  • 1
    yes next time give the url where you found the solution. [http://stackoverflow.com/a/32799659/3763848](http://stackoverflow.com/a/32799659/3763848) – Vasileios Pallas Apr 12 '17 at 13:30
  • Cant you do better with Picasso? Pretty strange that Picasso converts a jpg file to a bitmap first. Or a png file. Quite strange. It should just deliver the bytes of the file. Or yet better save the received bytes to file directly for you. Can it be done? – greenapps Apr 12 '17 at 13:40
  • `file.createNewFile();`. Better remove that. It serves nothing. – greenapps Apr 12 '17 at 13:42
  • `ostream.flush(); ostream.close();` Not needed. BitmapFactory has already done that. – greenapps Apr 12 '17 at 13:44