Android图片未下载(非常慢)

时间:2015-12-26 19:41:39

标签: android android-asynctask executorservice java-threads

实际上我在这里尝试从服务器下载多个图像,当我尝试从localhost下载图像时,它很好(在3秒内图像下载)。但是当我尝试从实际服务器下载它时,即使我的互联网连接非常快,它也需要很长时间(至少15秒)。图像的总大小不超过600 kb。

					pool = Executors.newFixedThreadPool(SplitImages.length);
					int length = 0;
					for (String name : SplitImages) {
						pool.submit(new DownloadImageSequencially(name,length));
						length = length+1;
					}

					pool.shutdown();
					try {
						if(!pool.awaitTermination(15, TimeUnit.SECONDS))
						{
							Log.e("TASKS NOT COMPLETED","TASKS NOT COMPLETED");
						}
					}
					catch (InterruptedException e)
					{
						Log.e("GONE","GONE");
					}

以上代码调用下面的AsyncTask方法

	private class DownloadImageSequencially implements Runnable
	{
		private String name;
		private int indx;

		public DownloadImageSequencially(String name,int indx) {
			this.name = name;
			this.indx=indx;
		}

		@Override
		public void run() {
			// surround with try-catch if downloadFile() throws something
			downloadBitmap(name);
		}

		private void downloadBitmap(String url) {
			// initilize the default HTTP client object

			HttpClient client = new DefaultHttpClient();

			//forming a HttoGet request
			url = url.replace("\\","");
			url = url.replace("\"","");
			Log.e("url","url: "+url);
			final HttpGet getRequest = new HttpGet(url);
			Log.e("Masthan1","Masthan1");
			//HttpPost getRequest = new HttpPost(url);
			try {

				if (android.os.Build.VERSION.SDK_INT > 9) {
					StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
					StrictMode.setThreadPolicy(policy);
				}
				Log.e("Masthan2","Masthan2");
				HttpResponse response = client.execute(getRequest);
				Log.e("Masthan3","Masthan3");
				//check 200 OK for success
				final int statusCode = response.getStatusLine().getStatusCode();
				Log.e("Masthan4","Masthan4");
				if (statusCode != HttpStatus.SC_OK) {
					Log.w("ImageDownloader", "Error " + statusCode +
							" while retrieving bitmap from " + url);

				}

				final HttpEntity entity = response.getEntity();
				Log.e("Masthan5","Masthan5");
				if (entity != null) {
					InputStream inputStream = null;
					try {
						// getting contents from the stream
						inputStream = entity.getContent();
						Log.e("Masthan6","Masthan6");
						// decoding stream data back into image Bitmap that android understands
						bitmapArray[indx] =  BitmapFactory.decodeStream(inputStream);
						Log.e("Masthan7","Masthan7");

					} finally {
						Log.e("Masthan8","Masthan8");
						if (inputStream != null) {
							Log.e("Masthan9","Masthan9");
							inputStream.close();
							Log.e("Masthan10", "Masthan10");
						}
						entity.consumeContent();
						Log.e("Masthan11", "Masthan11");
					}
				}
			} catch (Exception e) {
				// You Could provide a more explicit error message for IOException
				getRequest.abort();
				Log.e("ImageDownloader", "Something went wrong while" +
						" retrieving bitmap from " + url + e.toString());
			}


		}

	}

请帮助我为什么花这么长时间下载图片

logcat的:

12-27 00:54:59.387  10418-10484/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/approchableregardingmyconserns_11.png
12-27 00:54:59.387  10418-10484/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.388  10418-10484/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.390  10418-10488/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/ambienceisgoodforhavingfood_15.png
12-27 00:54:59.390  10418-10488/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.390  10418-10488/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.393  10418-10487/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/islargeenufftofitmein_14.png
12-27 00:54:59.393  10418-10487/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.393  10418-10487/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.395  10418-10486/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/ismaintainedcleanly_13.png
12-27 00:54:59.395  10418-10486/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.398  10418-10485/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/behavesprofessional_12.png
12-27 00:54:59.398  10418-10485/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.399  10418-10485/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.401  10418-10483/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/ishappytoserveme_10.png
12-27 00:54:59.401  10418-10483/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.401  10418-10483/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.403  10418-10482/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/serveshyginically_9.png
12-27 00:54:59.403  10418-10482/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.404  10418-10482/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.406  10418-10480/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/happywiththemenuplan_7.png
12-27 00:54:59.406  10418-10480/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.406  10418-10480/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.408  10418-10479/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/qualityofitemssufficient_6.png
12-27 00:54:59.408  10418-10479/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.409  10418-10479/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.412  10418-10478/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/menuitemsareavailable_5.png
12-27 00:54:59.412  10418-10478/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.412  10418-10478/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.424  10418-10477/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/valueformoney4.jpg
12-27 00:54:59.424  10418-10477/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.424  10418-10477/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.433  10418-10476/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/servedfresh3.jpg
12-27 00:54:59.433  10418-10476/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.433  10418-10476/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.438  10418-10475/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/healthy2.jpg
12-27 00:54:59.438  10418-10475/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.438  10418-10475/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.441  10418-10474/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/tasty1.jpg
12-27 00:54:59.441  10418-10474/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.441  10418-10474/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.466  10418-10481/com.revu.revu E/url﹕ url: http://abcd.co.in/revue/Images/ReviewCard/Student/servesasperthescedule_8.png
12-27 00:54:59.466  10418-10481/com.revu.revu E/Masthan1﹕ Masthan1
12-27 00:54:59.466  10418-10481/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.470  10418-10486/com.revu.revu E/Masthan2﹕ Masthan2
12-27 00:54:59.492  10418-10429/com.revu.revu I/art﹕ Background sticky concurrent mark sweep GC freed 4667(360KB) AllocSpace objects, 1(20KB) LOS objects, 2% free, 3MB/3MB, paused 5.438ms total 110.998ms
12-27 00:54:59.802  10418-10429/com.revu.revu I/art﹕ Background partial concurrent mark sweep GC freed 1558(103KB) AllocSpace objects, 0(0B) LOS objects, 25% free, 3MB/4MB, paused 8.296ms total 50.314ms
12-27 00:55:00.565  10418-10478/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:00.565  10418-10478/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:00.565  10418-10478/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:00.565  10418-10478/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:01.012  10418-10479/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:01.012  10418-10479/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:01.012  10418-10479/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:01.013  10418-10479/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:01.037  10418-10429/com.revu.revu I/art﹕ Background sticky concurrent mark sweep GC freed 33(1504B) AllocSpace objects, 0(0B) LOS objects, 0% free, 5MB/5MB, paused 10.228ms total 22.445ms
12-27 00:55:01.292  10418-10483/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:01.292  10418-10483/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:01.292  10418-10483/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:01.293  10418-10483/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:02.054  10418-10484/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:02.054  10418-10484/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:02.054  10418-10484/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:02.054  10418-10484/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:02.869  10418-10485/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:02.869  10418-10485/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:02.869  10418-10485/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:02.870  10418-10485/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:03.322  10418-10425/com.revu.revu W/art﹕ Suspending all threads took: 37.251ms
12-27 00:55:03.333  10418-10429/com.revu.revu I/art﹕ Background partial concurrent mark sweep GC freed 194(6KB) AllocSpace objects, 0(0B) LOS objects, 16% free, 10MB/12MB, paused 8.546ms total 42.975ms
12-27 00:55:04.052  10418-10487/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:04.052  10418-10487/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:04.052  10418-10487/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:04.053  10418-10487/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:05.073  10418-10474/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:05.096  10418-10474/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:05.097  10418-10474/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:05.097  10418-10474/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:06.095  10418-10478/com.revu.revu E/Masthan7﹕ Masthan7
12-27 00:55:06.095  10418-10478/com.revu.revu E/Masthan8﹕ Masthan8
12-27 00:55:06.095  10418-10478/com.revu.revu E/Masthan9﹕ Masthan9
12-27 00:55:06.105  10418-10478/com.revu.revu E/Masthan10﹕ Masthan10
12-27 00:55:06.105  10418-10478/com.revu.revu E/Masthan11﹕ Masthan11
12-27 00:55:06.295  10418-10425/com.revu.revu W/art﹕ Suspending all threads took: 21.464ms
12-27 00:55:06.721  10418-10475/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:06.721  10418-10475/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:06.722  10418-10475/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:06.722  10418-10475/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:06.771  10418-10488/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:06.771  10418-10488/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:06.771  10418-10488/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:06.771  10418-10488/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:06.810  10418-10429/com.revu.revu I/art﹕ Background sticky concurrent mark sweep GC freed 83(7KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 16MB/16MB, paused 5.049ms total 33.838ms
12-27 00:55:06.828  10418-10476/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:06.828  10418-10476/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:06.828  10418-10476/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:06.829  10418-10476/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:07.323  10418-10477/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:07.323  10418-10477/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:07.323  10418-10477/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:07.330  10418-10486/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:07.330  10418-10486/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:07.330  10418-10486/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:07.330  10418-10486/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:07.333  10418-10481/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:07.334  10418-10481/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:07.334  10418-10481/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:07.334  10418-10481/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:07.339  10418-10482/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:07.339  10418-10482/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:07.339  10418-10482/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:07.339  10418-10482/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:07.342  10418-10480/com.revu.revu E/Masthan3﹕ Masthan3
12-27 00:55:07.342  10418-10480/com.revu.revu E/Masthan4﹕ Masthan4
12-27 00:55:07.342  10418-10480/com.revu.revu E/Masthan5﹕ Masthan5
12-27 00:55:07.342  10418-10480/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:07.347  10418-10477/com.revu.revu E/Masthan6﹕ Masthan6
12-27 00:55:08.621  10418-10479/com.revu.revu E/Masthan7﹕ Masthan7
12-27 00:55:08.621  10418-10479/com.revu.revu E/Masthan8﹕ Masthan8
12-27 00:55:08.621  10418-10479/com.revu.revu E/Masthan9﹕ Masthan9
12-27 00:55:08.621  10418-10479/com.revu.revu E/Masthan10﹕ Masthan10
12-27 00:55:08.621  10418-10479/com.revu.revu E/Masthan11﹕ Masthan11
12-27 00:55:10.439  10418-10484/com.revu.revu E/Masthan7﹕ Masthan7
12-27 00:55:10.439  10418-10484/com.revu.revu E/Masthan8﹕ Masthan8
12-27 00:55:10.439  10418-10484/com.revu.revu E/Masthan9﹕ Masthan9
12-27 00:55:10.439  10418-10484/com.revu.revu E/Masthan10﹕ Masthan10
12-27 00:55:10.440  10418-10484/com.revu.revu E/Masthan11﹕ Masthan11
12-27 00:55:10.444  10418-10483/com.revu.revu E/Masthan7﹕ Masthan7
12-27 00:55:10.444  10418-10483/com.revu.revu E/Masthan8﹕ Masthan8
12-27 00:55:10.444  10418-10483/com.revu.revu E/Masthan9﹕ Masthan9
12-27 00:55:10.444  10418-10483/com.revu.revu E/Masthan10﹕ Masthan10
12-27 00:55:10.444  10418-10483/com.revu.revu E/Masthan11﹕ Masthan11
12-27 00:55:12.080  10418-10487/com.revu.revu E/Masthan7﹕ Masthan7
12-27 00:55:12.081  10418-10487/com.revu.revu E/Masthan8﹕ Masthan8
12-27 00:55:12.081  10418-10487/com.revu.revu E/Masthan9﹕ Masthan9
12-27 00:55:12.081  10418-10487/com.revu.revu E/Masthan10﹕ Masthan10
12-27 00:55:12.081  10418-10487/com.revu.revu E/Masthan11﹕ Masthan11
12-27 00:55:12.690  10418-10485/com.revu.revu E/Masthan7﹕ Masthan7
12-27 00:55:12.690  10418-10485/com.revu.revu E/Masthan8﹕ Masthan8
12-27 00:55:12.690  10418-10485/com.revu.revu E/Masthan9﹕ Masthan9
12-27 00:55:12.691  10418-10485/com.revu.revu E/Masthan10﹕ Masthan10
12-27 00:55:12.691  10418-10485/com.revu.revu E/Masthan11﹕ Masthan11
12-27 00:55:14.397  10418-10418/com.revu.revu E/TASKS NOT COMPLETED﹕ TASKS NOT COMPLETED

0 个答案:

没有答案