android程序内下载文件,在Android中下载文件

我使用以下代码在

Android中下载文件:

public class FileDownloadActivity extends Activity {

ProgressDialog mProgressDialog;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mProgressDialog = new ProgressDialog(FileDownloadActivity.this);

mProgressDialog.setMessage("Downlaoding File...");

mProgressDialog.setIndeterminate(false);

mProgressDialog.setMax(100);

mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

// execute this when the downloader must be fired

DownloadFile downloadFile = new DownloadFile();

downloadFile.execute("http://code.google.com/android/goodies/wallpaper/android-wallpaper5_1024x768.jpg");

}

private class DownloadFile extends AsyncTask {

@Override

protected String doInBackground(String... sUrl) {

try {

Log.i("","In doin bggggggggggggg");

URL url = new URL(sUrl[0]);

URLConnection connection = url.openConnection();

connection.connect();

// this will be useful so that you can show a typical 0-100% progress bar

int fileLength = connection.getContentLength();

// download the file

InputStream input = new BufferedInputStream(url.openStream());

OutputStream output = new FileOutputStream("/sdcard/sample.jpg");

byte data[] = new byte[1024];

long total = 0;

int count;

while ((count = input.read(data)) != -1) {

total += count;

// publishing the progress....

publishProgress((int) (total * 100 / fileLength));

output.write(data, 0, count);

}

output.flush();

output.close();

input.close();

} catch (Exception e) {

}

return null;

}

@Override

protected void onPreExecute() {

super.onPreExecute();

mProgressDialog.show();

}

@Override

protected void onProgressUpdate(Integer... progress) {

super.onProgressUpdate(progress);

mProgressDialog.setProgress(progress[0]);

}

}

}

这只是显示我过去半小时0%的进展.

我甚至补充道

在清单中

甚至互联网在我的手机中工作正常.

请帮忙.

谢谢

斯纳

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值