可能重复:
Android下载二进制文件问题
我尝试从 Internet 下载文件失败。服务启动时,下载文件的大小冻结在 325 B。我已获得 Internet 连接以及访问 Internet 和写入外部存储的权限。
public class DownloadService extends IntentService {
public DownloadService() {
super(null);
}
@Override
protected void onHandleIntent(Intent arg0) {
Log.d("service", "onHandleIntent()");
try {
URL fileUrl = new URL("http://goo.gl/Mfyya");
HttpURLConnection urlConnection = (HttpURLConnection)fileUrl.openConnection();
urlConnection.setRequestMethod("GET");
// urlConnection.setDoOutput(true);
urlConnection.connect();
File sdcard = new File (Environment.getExternalStorageDirectory()+"/my");
File file = new File (sdcard, "filename.mp4");
FileOutputStream fileoutput = new FileOutputStream (file);
InputStream inputstream = urlConnection.getInputStream();
// int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferlength =0;
while ((bufferlength = inputstream.read(buffer)) >= 0)
{
fileoutput.write(buffer, 0, bufferlength);
downloadedSize += bufferlength;
//updateProgress(downloadedSize, totalSize);
}
fileoutput.close();
} catch (IOException e) {
//Log.d("service", "error");
e.printStackTrace();
}
}
}
出了什么问题?