【发布时间】:2012-04-06 15:23:21
【问题描述】:
我会怎么做才能得到getContentLength
这是我的代码:
HttpGet request = new HttpGet(MyConstants.URL);
HttpResponse response = null;
response = httpClient.execute(request);
HttpEntity httpEntity = response.getEntity();
我正在尝试使我的代码与此示例代码类似:
示例代码:
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
【问题讨论】:
标签: android android-progressbar