3

i am working on an app in which i am using Picasso Library to Download image from URL.

Now there is a situation: i am able to download single image from URL: http://8020.photos.jpgmag.com/3456318_294166_528c960558_m.jpg

but not able to download multiple images from URL: https://www.dropbox.com/sh/5be3kgehyg8uzh2/AAA-jYcy_21nLBwnZQ3TBFAea

this is my code:

package com.example.imagedownloadsample;

import java.io.File;
import java.io.FileOutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.squareup.picasso.Callback.EmptyCallback;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

public class MainActivity extends ActionBarActivity {

    String currentUrl ="http://8020.photos.jpgmag.com/3456318_294166_528c960558_m.jpg";
    //String currentUrl = "https://www.dropbox.com/sh/5be3kgehyg8uzh2/AAA-jYcy_21nLBwnZQ3TBFAea";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_download);

        final ImageView img = (ImageView) (findViewById(R.id.imageView1));

        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.loading);

        Picasso.with(this).load(currentUrl).error(R.drawable.error_detail)
                .into(img, new EmptyCallback() {
                    @Override
                    public void onSuccess() {
                        progressBar.setVisibility(View.GONE);
                    }

                    @Override
                    public void onError() {
                        progressBar.setVisibility(View.GONE);
                    }
                });

        Picasso.with(this).load(currentUrl).into(target);

    }

    private Target target = new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            new Thread(new Runnable() {
                @Override
                public void run() {

                    String fileName = currentUrl.substring(
                            currentUrl.lastIndexOf('/') + 1,
                            currentUrl.length());

                    String fname = "/Android/data/com.usd.pop/image-"
                            + fileName;

                    File file = new File(Environment
                            .getExternalStorageDirectory().getPath() + fname);
                    if (file.exists())
                        file.delete();
                    try {
                        file.createNewFile();
                        FileOutputStream ostream = new FileOutputStream(file);
                        bitmap.compress(CompressFormat.JPEG, 100, ostream);
                        ostream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            if (placeHolderDrawable != null) {
            }
        }
    };

}

now my question is how do i download all images from this URL. https://www.dropbox.com/sh/5be3kgehyg8uzh2/AAA-jYcy_21nLBwnZQ3TBFAea

user3739970
  • 591
  • 2
  • 13
  • 28

3 Answers3

2

I may be being dumb, but as I believe you are using Java, may I suggest you use this tutorial.

This is some code to take all images from the website:

public class ExtractAllImages {
    public static void main(String args[]) throws Exception {

    String webUrl = "http://www.hdwallpapers.in/";
    URL url = new URL(webUrl);
    URLConnection connection = url.openConnection();
    InputStream is = connection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
    HTMLEditorKit.Parser parser = new ParserDelegator();
    HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
    parser.parse(br, callback, true);

    for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.IMG); iterator.isValid(); iterator.next()) {
        AttributeSet attributes = iterator.getAttributes();
        String imgSrc = (String) attributes.getAttribute(HTML.Attribute.SRC);

        if (imgSrc != null && (imgSrc.endsWith(".jpg") || (imgSrc.endsWith(".png")) || (imgSrc.endsWith(".jpeg")) || (imgSrc.endsWith(".bmp")) || (imgSrc.endsWith(".ico")))) {
            try {
                downloadImage(webUrl, imgSrc);
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
}
private static void downloadImage(String url, String imgSrc) throws IOException {
    BufferedImage image = null;
    try {
        if (!(imgSrc.startsWith("http"))) {
            url = url + imgSrc;
        } else {
            url = imgSrc;
        }
        imgSrc = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
        String imageFormat = null;
        imageFormat = imgSrc.substring(imgSrc.lastIndexOf(".") + 1);
        String imgPath = null;
        imgPath = "C:/Users/Machine2/Desktop/CTE/Java-WebsiteRead/" + imgSrc + "";
        URL imageUrl = new URL(url);
        image = ImageIO.read(imageUrl);
        if (image != null) {
            File file = new File(imgPath);
            ImageIO.write(image, imageFormat, file);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}}
Alexander Craggs
  • 7,874
  • 4
  • 24
  • 46
  • bro thanx for your answer but can i use it in android or will it work in android – user3739970 Aug 10 '14 at 16:44
  • If I'm honest I'm not entirely sure. I thought android was written in java, however this isn't using Picasso, like yours does. I have never designed for android and so I can't test it. – Alexander Craggs Aug 11 '14 at 13:12
1

Pretty sure you need to know the name of all images you are retrieving.

So, you cannot retrieve all from a url, instead you will need to know the name of each of image, and step through an array or something to retrieve them individually.

Pretty sure you cannot do this:

Picasso.with(this).load(http://someurl.com/photos/*).into(target);
Booger
  • 18,579
  • 7
  • 55
  • 72
0

First you need to obtain response images from that URL using Volley library or some other j son parsing library and then put them in array , your array should look like images URL array. then with each position of images you can load it with Picasso . thanks

Chiranjhivi Ghimire
  • 1,739
  • 1
  • 19
  • 21