3
Android 개발을 처음 사용했습니다.Android에서 파일 다운로드 진행률 표시 줄 구현
나는 인터넷에서 비디오 파일을 다운로드하기 위해 다음 코드를 작성했습니다. 그것은 잘 작동합니다. 이제 다운로드 프로세스 중에 진행률 표시 줄을 첨부하고 싶습니다. AsyncTask를 서브 클래스 화하고 doInBackground() 메소드 내에 다운로드 코드를 작성하려고했습니다. 그러나 어쨌든 나는 그것을 이해할 수 없다.
누군가이 코드를 수정하면 도움이 될 수 있습니까?
package sample.android.download;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;
public class DownloadDemo extends Activity {
private TextView tv;
private String videoURL = "http://mysite-name.com/videos/videofile_name.mp4";
private String fileName = "my_video.mp4";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.TextView01);
if(checkExternalMedia()==true) {
DownloadFromUrl(videoURL,fileName);
tv.append("\n\nDownload Complete!");
}
else {
tv.append("\n\nExternal Media is NOT readable/writable");
}
}
/** Method to check whether external media available and writable. */
private boolean checkExternalMedia(){
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
boolean stat;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// Can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
stat = true;
}
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
stat = false;
} else {
// Can't read or write
mExternalStorageAvailable = mExternalStorageWriteable = false;
stat = false;
}
tv.append("\n\nExternal Media: readable="+mExternalStorageAvailable+ "writable="+mExternalStorageWriteable);
return stat;
}
/** Method to download an external file from the network to the SD card. */
public void DownloadFromUrl(String videoURL, String fileName) {
try {
File root = android.os.Environment.getExternalStorageDirectory();
tv.append("\nExternal file system root: "+root);
File dir = new File (root.getAbsolutePath() + "/video");
//dir.mkdirs();
URL url = new URL(videoURL); //you can write here any link
File file = new File(dir, fileName);
long startTime = System.currentTimeMillis();
Log.d("ImageManager", "download begining");
Log.d("ImageManager", "download url:" + url);
Log.d("ImageManager", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
Log.d("ImageManager", "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + " sec");
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
}
}
'AsyncTask'의 어느 부분에서 문제가 있습니까? [documentation] (http://developer.android.com/reference/android/os/AsyncTask.html) 및 [example] (http://developer.android.com/resources/articles/painless-threading.html) 합리적으로 명확합니다. 아마도 당신은 가지고있는 문제를 설명 할 수 있습니까? –
가능한 중복 [Android로 파일을 다운로드하고 ProgressDialog에서 진행 상황을 표시] (http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress- in-a-progressdialog) –
요구 사항이 비슷하지만 AsyncTask를 구현할 수 없었습니다. – Sourav