private class DownloadLargeFileTask extends AsyncTask<Void, Void, Void>
{
private final ProgressDialog dialog;
public DownloadLargeFileTask(ProgressDialog dialog) {
this.dialog = dialog;
}
protected void onPreExecute() {
dialog.show();
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
@Override
protected Void doInBackground(Void... arg0) {
download();
return null;
}
}
private void download()
{
try
{
long startTime = System.currentTimeMillis();
URL u = new URL("http://mds.podfm.ru/188/download/040_Sergejj_Palijj_-_Karantin_CHast_2.mp3");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File("/sdcard/","my.mp3"));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
int downloadedSize = 0;
while ((len1 = in.read(buffer)) != -1)
{
f.write(buffer,0, len1);
downloadedSize += len1;
ReturnDownloadedBytes(downloadedSize);
}
f.close();
Log.d("ImageManager", "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + " sec");
}
catch (IOException e)
{
Log.d("ImageManager", "Error" + ((System.currentTimeMillis())/1000) + e + " sec");
}
}
private void ReturnDownloadedBytes(int size)
{
text.setText(String.valueOf(size));
}
錯誤說:只有創建視圖層次結構的原始線程可以觸及其視圖。 我認爲這意味着我從一個therad創建textview並嘗試從另一個(AsyncTask)獲取訪問權限,但是如何獲取它? 感謝試圖獲得下載文件的大小,但得到一個錯誤
編輯
這裏是所有代碼。但即使我在publishProgress(downloadedSize) int value = 10
發送alwys在text.setText(String.valueOf(progress));
不同值輸出像[Ljava.lang.float;@43ed62f78
public class list extends Activity {
private TextView text;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.result);
}
public void selfDestruct(View view) {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("????????. ?????...");
new DownloadLargeFileTask(dialog).execute();
}
private class DownloadLargeFileTask extends AsyncTask<Void, Integer, Void>
{
private final ProgressDialog dialog;
public DownloadLargeFileTask(ProgressDialog dialog) {
this.dialog = dialog;
}
protected void onPreExecute() {
dialog.show();
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
protected void onProgressUpdate(Integer...progress) {
text.setText(String.valueOf(progress));
}
@Override
protected Void doInBackground(Void... arg0) {
try
{
long startTime = System.currentTimeMillis();
URL u = new URL("http://mds.podfm.ru/188/download/040_Sergejj_Palijj_-_Karantin_CHast_2.mp3");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File("/sdcard/","my.mp3"));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
int downloadedSize = 10;
while ((len1 = in.read(buffer)) != -1)
{
f.write(buffer,0, len1);
//downloadedSize += len1;
**publishProgress(downloadedSize);**
}
f.close();
Log.d("ImageManager", "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + " sec");
}
catch (IOException e)
{
Log.d("ImageManager", "Error" + ((System.currentTimeMillis())/1000) + e + " sec");
}
return null;
}
}
你可以給我解釋一下什麼是 「網址,爲Integer,Long」 是指在這個「私人類DownloadFilesTask擴展AsyncTask「,以及這個params /的順序有什麼不同。感謝 –
SERG
,當我調用text.setText(String.valueOf(progress));結果我在TextView中得到了類似[Ljava.lang.float; @ 43ed62f78 - 這是什麼意思? – SERG
@SERG檢查[documentation](http://developer.android.com/reference/android/os/AsyncTask.html),它們是'Params','Progress'和'Result'。我需要看到更多的代碼來診斷其他問題。 –