Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download and Save Images Using Picasso

I'm trying to show my news in a custom ListView. Each news is included of some images and I want to

1.download images from server

2.save in local storage

3.save path of images into SQLite

4.show images in ListView using my custom adapter.

I just have problem with steps 1 & 2. I can get news from server and show them in my ListView

and show images from cache by add below code in my adapter:

Picasso.with(context).load(image[position]).into(iv);

By using Picasso.with(context).load(image[position]).into(target) , just I can save one

image in storage.

Please suggest me your idea ...

UPDATE: When I use below code, just one image (last index of my image array) being saved!

How can I save all images in array with this code?!

@Override
protected void onPostExecute(Void result) {
   SaveImages();
   pDialog.dismiss();
   super.onPostExecute(result);
}

String fileName = null;

public void SaveImages() {
    for(int i = 0; i < image.length; i++) {
        Picasso.with(this).load(image[i]).into(target);
        fileName = "image-" + i + ".jpg";
    }
}

Target target = new Target() {

    @Override
    public void onPrepareLoad(Drawable arg0) {
    }

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

    @Override
    public void onBitmapFailed(Drawable arg0) {
    }
};
like image 810
Farshad Kazemi Avatar asked Jan 01 '15 10:01

Farshad Kazemi


People also ask

How do you load an image into an imageView from an image URL using Picasso?

Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.

How do I use Android Picasso library to download images?

This example demonstrates how do I use the Android Picasso library to download images. Step 1 − Create a new project in Android Studio, go to File ⇒New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java

How to use Picasso to save image file in Java?

To use the Picasso for saving image file, you need to define a Target class. This method creates a target object that you can use with Picasso. Target is an interface defined in Picasso’s library. You can define this method in your Activity class or an util class.

How to use Picasso to save image file in Gradle?

Make sure you have picasso in your gradle build file’s dependencies tag. 2. To use the Picasso for saving image file, you need to define a Target class. This method creates a target object that you can use with Picasso. Target is an interface defined in Picasso’s library. You can define this method in your Activity class or an util class. 3.

Why can't I view large images in Picasso?

Note: there is a bug with the current version of Picasso that prevents large images (i.e. 10MB) from being loaded, especially with newer camera phones that have larger resolutions. If you are experiencing this issue, you may need to upgrade to the Picasso 2.6.0 snapshot. See the troubleshooting guide to confirm.


1 Answers

Try to put Target target definition before call to Picasso.with(this).load(image[i]).into(target);

P.S. Using the following code and I saved images very well. Thanks, anyway.

My Code:

        final String fileName = mDataset.get(i).getAid() + ".jpg";
        Target target = new Target() {

            @Override
            public void onPrepareLoad(Drawable arg0) {
                return;
            }

            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) {

                try {
                    File file = null;

                    // judge "imgs/.nomedia"'s existance to judge whether path available
                    if(LightCache.testFileExist(GlobalConfig.getFirstStoragePath()
                            + "imgs" + File.separator +".nomedia") == true)
                        file = new File(GlobalConfig.getFirstStoragePath()
                                + "imgs" + File.separator + fileName);

                    else file = new File(GlobalConfig.getSecondStoragePath()
                            + "imgs" + File.separator + fileName);

                    file.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
                    ostream.close();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

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

        Picasso.with(GlobalConfig.getContext())
                .load(Wenku8API.getCoverURL(mDataset.get(i).getAid()))
                .into(target);
like image 164
MewX Avatar answered Oct 08 '22 09:10

MewX