android AsyncTask 비동기 다운로드 및 진행 항목 업데이트
AsyncTask
//
AsyncTask<String, Integer, String>
:String
:Integer
:String , , BitMap
:
package com.example.testcctv;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.HttpStatus;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ProgressBar;
/*****************************************************************************************************************************************
* : :String :Integer :String
* , , BitMap
*****************************************************************************************************************************************/
public class DownloadAsyncTask extends AsyncTask<String, Integer, String> {
private final ProgressBar bar;
private int count = 0;
public DownloadAsyncTask(ProgressBar bar) {
super();
this.bar = bar;
}
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(10 * 1000);
connection.connect();
if (connection.getResponseCode() == HttpStatus.SC_OK) {
bar.setMax(connection.getContentLength());
File file = new File(params[1]);
file.createNewFile();
InputStream inputStream = connection.getInputStream();
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[10 * 1024];
while (true) {
int len = inputStream.read(buffer);
publishProgress(len);
if (len == -1) {
break;
}
arrayOutputStream.write(buffer, 0, len);
}
arrayOutputStream.close();
inputStream.close();
byte[] data = arrayOutputStream.toByteArray();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(data);
fileOutputStream.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return " ";
}
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
}
@Override
protected void onPostExecute(String result) {
Log.v("Fover", result);
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... values) {
count += values[0];
bar.setProgress(count);
super.onProgressUpdate(values);
}
}
:
// ,
String[] downLoadPath = {
HTTPURL,
Environment.getExternalStorageDirectory()
+ "/dujinyang/dudu.apk" };
DownloadAsyncTask asynTask = new DownloadAsyncTask(progressBar);
asynTask.execute(downLoadPath);
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.