Android下载大文件 [英] Android download large files
问题描述
我正在尝试从网络服务器下载一个大的.zip文件,但我有一个奇怪的行为,描述是:
I'm trying to download a large .zip file from a web server but I have a weird behaviour, the description is:
- 我正在使用512MB的SD卡在设备模拟器API API 3(版本1.5)中执行代码。我使用擦除数据用户启动设备
- conexion.getContentLength()的大小长度为7012725
- 服务器地址为localhost (10.0.2.2),但我尝试使用外部服务器,行为是一样的。我已经检查过,我可以通过网络浏览器下载文件。
- 我在清单文件中有这些权限:
- I'm executing the code in the device emulator, API level 3 (version 1.5) with an SD card of 512MB. I start the device with "Wipe data user"
- The length of the size from conexion.getContentLength() is 7012725
- The server address is localhost (10.0.2.2), but I've tried with an external server and the behaviour is the same. I've double checked that I can download the file through a web browser.
- I have these permisions in the manifest file:
-
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
错误:
- 它开始下载文件,我可以看到文本10,20,30,40,50,然后在60秒停止。
- 一段时间后,仿真器重新启动
解决方法:
- stackoverflow中的问题在设备中正常工作,而不是我的情况。
-
关于此wifi锁定问题我所做的是添加此权限android.permission.WAKE_LOCK然后这段代码,但具有完全相同的行为:
- This issue in stackoverflow is working in the device, not in my case.
About this wifi lock issue what I've done is add this permission "android.permission.WAKE_LOCK" and then this piece of code, but with exactly the same behaviour:
WifiManager.WifiLock wifilock;
WifiManager manager =(WifiManager)getSystemService(Context.WIFI_SERVICE);
wifilock = manager.createWifiLock(wifilock);
wifilock.acquire();
...
wifilock.release();
WifiManager.WifiLock wifilock; WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE); wifilock = manager.createWifiLock("wifilock"); wifilock.acquire(); ... wifilock.release();
这是代码,它正在单独的线程中执行:
Here is the code, that it's being executing in a separate thread:
private void downloadData(){
try{
Log.v(TAG, "downloading data");
URL url = new URL("http://10.0.2.2/1.zip");
URLConnection connection = url.openConnection();
connection.connect();
int lenghtOfFile = connection.getContentLength();
Log.v(TAG, "lenghtOfFile = "+lenghtOfFile);
InputStream is = url.openStream();
File testDirectory = new File(Environment.getExternalStorageDirectory()+"/testDirectory/");
if(!testDirectory.exists()){
testDirectory.mkdir();
}
FileOutputStream fos = new FileOutputStream(testDirectory+"/files.zip");
byte data[] = new byte[1024];
int count = 0;
long total = 0;
int progress = 0;
while ((count=is.read(data)) != -1)
{
total += count;
int progress_temp = (int)total*100/lenghtOfFile;
if(progress_temp%10 == 0 && progress != progress_temp){
progress = progress_temp;
Log.v(TAG, "total = "+progress);
}
fos.write(data, 0, count);
}
is.close();
fos.close();
Log.v(TAG, "downloading finished");
}catch(Exception e){
Log.v(TAG, "exception in downloadData");
e.printStackTrace();
}
}
任何线索?
非常感谢你。
Any clue? Thank you very much.
更新更多的日志描述:
克里斯,我试图发现什么
正在进行,这个日志在
Eclipse环境中。这是一个
更多的细节,关于什么是
发生(时间 - 行动),请注意
我已经更改文件下载
另一个在别的做更多
测试,但结果是相当的
相同:
Hi Chris, I've tried to discover what was going on at first with this log in the Eclipse environment. Here are a little bit more detail about what is happening (time - action), note that I've changed the file to download for another one in orther to do more tests, but the results are quite the same:
00:00 It starts downloading:
V/iPhoto ( 853): downloading data
V/iPhoto ( 853): lenghtOfFile = 7732809
V/iPhoto ( 853): total = 10
V/iPhoto ( 853): total = 20
V/iPhoto ( 853): total = 30
V/iPhoto ( 853): total = 40
V/iPhoto ( 853): total = 50 (round 00:05)
-> Here it stops and the DDMS disconnects the device immediately
03:40 It finish the download (not always) and the device reboots on its own:
I/Process ( 595): Sending signal. PID: 595 SIG: 3
I/dalvikvm( 595): threadid=7: reacting to signal 3
D/dalvikvm( 722): GC freed 2193 objects / 135808 bytes in 176 sec
V/iPhoto ( 853): total = 60
I/dalvikvm( 595): Wrote stack trace to '/data/anr/traces.txt'
I/ActivityManager( 595): Process com.google.android.apps.maps:FriendService (pid 778) has died.
I/ActivityManager( 595): Process com.android.mms (pid 732) has died.
V/iPhoto ( 853): total = 70
V/iPhoto ( 853): total = 80
V/iPhoto ( 853): total = 90
V/iPhoto ( 853): total = 100
V/iPhoto ( 853): downloading finished
V/iPhoto ( 853): thread finish loading
I/Process ( 595): Sending signal. PID: 595 SIG: 9
I/ActivityThread( 757): Removing dead content provider: settings
I/ActivityThread( 748): Removing dead content provider: settings
I/ActivityThread( 722): Removing dead content provider: settings
I/ActivityThread( 700): Removing dead content provider: settings
I/ServiceManager( 549): service 'package' died
... services dying...
I/ServiceManager( 549): service 'wifi' died
E/installd( 557): eof
E/installd( 557): failed to read size
I/installd( 557): closing connection
D/qemud ( 560): fdhandler_event: disconnect on fd 11
D/qemud ( 560): fdhandler_event: disconnect on fd 12
E/vold ( 550): Framework disconnected
I/Zygote ( 554): Exit zygote because system server (595) has terminated
I/ServiceManager( 549): service 'simphonebook' died
I/ServiceManager( 549): service 'isms' died
I/ServiceManager( 549): service 'iphonesubinfo' died
I/ServiceManager( 549): service 'phone' died
D/AndroidRuntime( 949):
D/AndroidRuntime( 949): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
... and starting ...
任何人都可以尝试我的代码并下载zip文件大小为8MB?这是否适用于您?
Could anyone try my code and to download a zip file with size round 8MB? Does it works for you?
推荐答案
我刚刚在一个真实的设备中尝试过这个代码,它的工作原理很好。仿真器中必须有奇怪的东西。
I've just tried this code in a real device and it works perfectly. There must be something weird in the emulator.
这篇关于Android下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!