android下载多个图像(使用URL连接)

时间:2016-09-17 03:58:15

标签: android url

我正在尝试下载给定网址的多个图片。我有以下代码,我坚持。有人可以帮我将折旧的http转换为URL吗?

            for (int i = 0; i < URLS.length; i++) {
                try {
                    File firstFile = new  File(directory + "/" + i + ".png");
                    if (firstFile.exists()==false)
                    {
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpGet httpGet = new HttpGet(URLS[i]);
                        HttpResponse resp = httpClient.execute(httpGet);

                        if(resp.getStatusLine().getStatusCode()==200){
                            HttpEntity entity = resp.getEntity();
 InputStream is = entity.getContent();

                            Boolean status = firstFile.createNewFile();

                            FileOutputStream fouts = new FileOutputStream(firstFile);
                            byte[] buffer = new byte[1024];
                            long total = 0 ;
                            int count;

                            while ((count = is.read(buffer))!=-1){
                                total+=count;
                                fouts.write(buffer,0,count);
                            }
                            fouts.close();
                            is.close();
                            publishProgress(i);
                        }
                    }
                }
                catch(MalformedURLException e){e.printStackTrace();}

                catch(IOException e){e.printStackTrace();}
            }
            return null;
        }
        protected void onProgressUpdate(Object... values){
            super.onProgressUpdate(values);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

使用Glide或picasso库可以更轻松地下载图像,这些是下载图像的速度更快的库。 Check this tutorial