android AsyncTask 使用 HttpURLConnection lenghtOfFile 为 -1 需要帮助
我正在尝试使用 AsyncTask
在后台下载 mp 文件到模拟器中,它可以正常工作..但在设备中它不显示 progressbar
这个问题是由于当我运行这段代码时,
int lenghtOfFile = c.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
它给了我-1,为什么会发生这种情况?我的代码是
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
// mp3load();
int len1 = 0;
int count;
try {
URL url = new URL(GlobalVariable.Getstr());
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lenghtOfFile = c.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.v(LOG_TAG, "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
// String fileName = "workTest.mp3";
String fileName = GlobalVariable.Getstrpath().toString();
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
long total = 0;
while ((count = is.read(buffer)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// fos.write(buffer, 0, len1);
fos.write(buffer, 0, count);
}
fos.close();
is.close();
} catch (IOException e) {
Log.d(LOG_TAG, "Error: " + e);
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC", progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
refreshList();
Toast.makeText(
FindFilesByType.this,
"Downloading of " + GlobalVariable.Getstrpath()
+ " complete.", Toast.LENGTH_LONG).show();
// Intent i = new Intent(FindFilesByType.this, SongList.class);
// finish();
// startActivity(i);
try{
Intent i = new Intent(FindFilesByType.this, SongList.class);
finish();
startActivity(i);}catch (Exception e) {
// TODO: handle exception
}
}
}
整个代码适用于模拟器
但它总是不显示我的进度,但下载成功
谢谢 Pragna
i am trying to download mp file in back ground using AsyncTask
into the emulator it works properly.. but in divice it doesn't show the progressbar
this problem is due to the when i am running this code the
int lenghtOfFile = c.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
that gives me -1 why this happning? my code is
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
// mp3load();
int len1 = 0;
int count;
try {
URL url = new URL(GlobalVariable.Getstr());
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lenghtOfFile = c.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.v(LOG_TAG, "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
// String fileName = "workTest.mp3";
String fileName = GlobalVariable.Getstrpath().toString();
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
long total = 0;
while ((count = is.read(buffer)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// fos.write(buffer, 0, len1);
fos.write(buffer, 0, count);
}
fos.close();
is.close();
} catch (IOException e) {
Log.d(LOG_TAG, "Error: " + e);
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC", progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
refreshList();
Toast.makeText(
FindFilesByType.this,
"Downloading of " + GlobalVariable.Getstrpath()
+ " complete.", Toast.LENGTH_LONG).show();
// Intent i = new Intent(FindFilesByType.this, SongList.class);
// finish();
// startActivity(i);
try{
Intent i = new Intent(FindFilesByType.this, SongList.class);
finish();
startActivity(i);}catch (Exception e) {
// TODO: handle exception
}
}
}
the entiere code works fine for emulator
but it always doesn't display me progress but download happens sucessfully
thanks Pragna
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有时java无法检索远程文件的大小,我认为这是你的问题。
但调试代码时请检查手机的网络连接。
Somethimes java can't retrive the size of a remote file, and i think that is your problem.
But please check internet connection on your phone when debugging the code.
遇到了同样的问题(仅限 Android 4.0),并
为我删除修复了它。
Got the same problem (on Android 4.0 only), and removing
fixed it for me.