¾È±Ô °øºÎ¹æ

Android > AsyncTask - ÇÁ·Î±×·¹½Ãºê¹Ù¸¦ ÀÌ¿ëÇÑ ÆÄÀÏ ´Ù¿î·Îµå ±¸Çö

µî·ÏÀÏ : 2017-07-06 17:24 Á¶È¸¼ö : 53,352

ÀÎÅͳÝÀ̳ª ƯÁ¤Àå¼Ò¿¡¼­ ÆÄÀÏÀ» ´Ù¿î¹Þ°íÀÚ ÇÒ ¶§ AsyncTask Ŭ·¡½º¿Í  ÇÁ·Î±×·¹½Ãºê¹Ù À§Á¬À» ÀÌ¿ëÇØ ±¸ÇöÇÑ´Ù.

¿Ö³ÄÇÏ¸é ¿ë·®ÀÌ Å¬ °æ¿ì¿¡´Â  ¹Ù·Î ¹ÞÀ»¼ö ¾ø°í ´Ù ¹ÞÀ» ¶§ ±îÁö ±â´Ù·Á¾ß Çϱ⠶§¹®ÀÌ´Ù.

±â´Ù¸®´Â µ¿¾È ÇÁ·Î±×·¹½Ãºê¹Ù¸¦ ÀÌ¿ëÇØ ´Ù¸¥ °ÍÀº Á¶ÀÛÇÒ¼ö ¾øµµ·Ï ¸ð´Þ ´ÙÀ̾ó·Î±×ÇüÅ·Π¶ç¿ì´Â °ÍÀÌ´Ù.

ÀÌ°ÍÀº ´Ù¾çÇÑ ÇüÅ·ΠÀÀ¿ëÀÌ °¡´ÉÇÏ´Ù. ÆÄÀÏ »Ó¸¸ ¾Æ´Ï¶ó ½Ã°£ÀÌ °É¸®´Â ÀÛ¾÷ÀÏ °æ¿ì ÀÚÁÖ ÀÌ¿ëµÇ°í Àֱ⠶§¹®ÀÌ´Ù.

À̹ø ¿¹Á¦´Â ÀÎÅͳݿ¡ ¿¬°áµÇ¾î ÀÖ´Â ±×¸²ÀÇ URL À» °¡Áö°í ³×Æ®¿öÅ©·Î Á¢¼ÓÇÑÈÄ sdcard ¿¡ ÀúÀåÇÏ´Â ¿¹Á¦ÀÌ´Ù.


1. Main activity

¸ÞÀÎÀº °£´ÜÇÏ´Ù. ¹öÆ°Çϳª¸¸ ÀÖÀ¸¸ç Ŭ¸¯ÇÏ¸é ´Ù¿î¹ÞÀ» ÆÄÀÏÀÇ URL À» ³Ñ°ÜÁÖ°í DownloadFileAsync Ŭ·¡½ºÀÇ execute() ÇÔ¼ö¸¦ ½ÇÇà½ÃÄÑ ÁØ´Ù.
 
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			startDownload();
			break;
	 
		default:
			break;
		}
	}
	 
	private void startDownload() {
		String url = "http://cfs11.blog.daum.net/image/5/blog/2008/08/22/18/15/48ae83c8edc9d&filename=DSC04470.JPG";
		new DownloadFileAsync(this).execute(url, "1", "1");
	} 
	


2. ÆÄÀÏ ´Ù¿î·Îµå¸¦ À§ÇÑ AsyncTask Ŭ·¡½º

¿©±â¼­´Â Å©°Ô 4ºÎºÐÀ¸·Î ±¸ÇöµÇ´Âµ¥ AsyncTask °´Ã¼°¡ ½ÃÀ۵Ǵ ºÎºÐÀÎ onPreExecute(), ½ÃÀÛÈÄ µ¹¾Æ°¡´Â Á߽ɷÎÁ÷ÀÌ µé¾î°¡´Â ÇÔ¼ö doInBackground(),

½Ç½Ã°£À¸·Î ÀÛ¾÷°á°ú¸¦ ÇÁ·Î±×·¹½Ãºê¹Ù¿¡ ¾÷µ¥ÀÌÆ® ÇÏ´Â onProgressUpdate(), ±×¸®°í ¸¶Áö¸·À¸·Î Á¾·áÇÒ ¶§ ÇÁ·Î±×·¹½Ãºê¹Ù¸¦ »ç¶óÁö°Ô ÇÏ´Â onPostExcute()

·Î ±¸¼ºµÇ¾îÀÖ´Ù.

ÇÁ·Î±×·¹½Ãºê¹ÙÀÇ °´Ã¼¸¦ »ý¼ºÇÏ°í È­¸é¿¡ º¸¿©ÁØ´Ù.
 
		@Override
		protected void onPreExecute() {
			mDlg = new ProgressDialog(mContext);
			mDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mDlg.setMessage("Start");
			mDlg.show();
		 
			super.onPreExecute();
		}


¹ÞÀº URL Á¤º¸·Î »çÀÌÆ®¿¡ Á¢¼ÓÇÑÈÄ ÆÄÀÏÀ» ´Ù¿î¹Þ´Âµ¥ InputStream À¸·Î ¿¬ÈÄ OutputStream À¸·Î ÇØ´çÇÏ´Â ÆÄÀÏÀ» ¹ÙÀÌÆ®·Î Àоî sdcard ¿¡ ÀúÀåÇÑ´Ù.

ÇöÀç´Â À̸§ÀÌ Á¤ÇØÁ® ÀÖ´Ù.

 
		@Override
		protected String doInBackground(String... params) {
		 
			int count = 0;
			 
			try {
				Thread.sleep(100);
				URL url = new URL(params[0].toString());
				URLConnection conexion = url.openConnection();
				conexion.connect();
		 
				int lenghtOfFile = conexion.getContentLength();
				Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
		 
				InputStream input = new BufferedInputStream(url.openStream());
				OutputStream output = new FileOutputStream("/sdcard/Downloadtest.jpg");
		 
				byte data[] = new byte[1024];
		 
				long total = 0;				
				int progress = 0;
		 
				while ((count = input.read(data)) != -1) {
					total += count;

					int progress_temp = (int) total * 100 / lenghtOfFile;

					// ÀÛ¾÷ÀÌ ÁøÇàµÇ¸é¼­ È£ÃâÇϸç È­¸éÀÇ ¾÷±×·¹À̵带 ´ã´çÇÏ°Ô µÈ´Ù
					publishProgress("progress", String.valueOf(progress_temp),"´Ù¿îÁßÀÔ´Ï´Ù....");
					
					if (progress_temp % 10 == 0 && progress != progress_temp) {
						progress = progress_temp;
					}

					output.write(data, 0, count);
				}
		 
				output.flush();
				output.close();
				input.close();				 
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		 
			// ¼öÇàÀÌ ³¡³ª°í ¸®ÅÏÇÏ´Â °ªÀº ´ÙÀ½¿¡ ¼öÇàµÉ onProgressUpdate ÀÇ ÆĶó¹ÌÅÍ°¡ µÈ´Ù
			return null;
		}
		


½Ç½Ã°£À¸·Î ´Ù¿î¹ÞÀº ÁøÇà »çÇ×À» ¾÷µ¥ÀÌÆ® ÇÑ´Ù.
 
		@Override
		protected void onProgressUpdate(String... progress) {
			if (progress[0].equals("progress")) {
				mDlg.setProgress(Integer.parseInt(progress[1]));
				mDlg.setMessage(progress[2]);
			} else if (progress[0].equals("max")) {
				mDlg.setMax(Integer.parseInt(progress[1]));
			}
		}
		


doInBackground ÇÔ¼ö°¡ Á¾·áµÇ¾ú´Ù¸é ´ÙÀ½ ÇÔ¼ö°¡ È£ÃâµÇ´Âµ¥ ¿©±â¿¡¼­ ÇÁ·Î±×·¹½Ãºê¹Ù¸¦ Á¾·á ÇÑ´Ù.
 
		@SuppressWarnings("deprecation")
		@Override
		protected void onPostExecute(String unused) {
			mDlg.dismiss();
			 
		}
 
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.