Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 毕加索从URL下载多幅图像_Android_Url_Android Image_Picasso_Android Internet - Fatal编程技术网

Android 毕加索从URL下载多幅图像

Android 毕加索从URL下载多幅图像,android,url,android-image,picasso,android-internet,Android,Url,Android Image,Picasso,Android Internet,我正在开发一个应用程序,我正在使用毕加索库从URL下载图像 现在有一种情况:我可以从URL下载单个图像: 但无法从URL下载多个图像: 这是我的代码: package com.example.imagedownloadsample; import java.io.File; import java.io.FileOutputStream; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressForma

我正在开发一个应用程序,我正在使用
毕加索库
从URL下载图像

现在有一种情况:我可以从URL下载单个图像:

但无法从URL下载多个图像:

这是我的代码:

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) {
            }
        }
    };

}
现在我的问题是如何从这个URL下载所有图片。

非常确定您需要知道正在检索的所有图像的名称

因此,您无法从url中检索所有内容,而是需要知道每个图像的名称,并逐步通过数组或其他方式分别检索它们

很确定你不能这样做:

Picasso.with(this).load(http://someurl.com/photos/*).into(target);

我可能很傻,但我相信你们正在使用Java,我建议你们使用教程

这是从网站上获取所有图像的一些代码:

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

}}

首先,您需要使用Volley库或其他j-son解析库从该URL获取响应图像,然后将它们放入数组中,您的数组应该类似于图像URL数组。然后,你可以用毕加索将图像的每个位置载入。谢谢

是否有人可以回答,等待16小时的回答我知道这并不容易,这就是为什么我问这样一种方法,从urlbro thanx检索所有图像以获得您的答案,但我可以在android中使用它,还是在android中工作?如果我诚实,我不完全确定。我原以为android是用java写的,但它不像你的一样使用毕加索。我从来没有为android设计过,所以我无法测试它。