Android的下载大型文件文件、Android

2023-09-06 01:04:44 作者:?十年浮夸终成K歌之王

我试图下载来自Web服务器的大型.zip文件,但我有一个怪异的行为,描述是:

在我执行code在设备仿真器,API级别3(版本1.5)与512MB的SD卡。我开始将设备与擦除数据的用户 尺寸从conexion.getContentLength()的长度为7012725 在服务器地址为localhost(10.0.2.2),但我已经试过与外部服务器的行为是一样的。我已经双重检查,我可以通过网络浏览器下载文件。 在我的这些permisions清单文件中:

-

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对>< /使用-许可>
<使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E>< /使用-许可>
 

错误:

它开始下载文件,我可以看到文本10,20,30,40,50,然后将其在60停止。 在一段时间后,模拟器自动重启。

解决方法:

在计算器这的问题是工作中的设备,而不是在我的情况。

关于我所做的这个无线网络锁问题是添加此权限android.permission.WAKE_LOCK,然后这块code,但具有完全相同的行为:

WifiManager.WifiLock wifilock; WifiManager经理=(WifiManager)getSystemService(Context.WIFI_SERVICE); wifilock = manager.createWifiLock(wifilock); wifilock.acquire(); ... wifilock.release();

下面是code,即它是在执行一个单独的线程:

 私人无效downloadData(){
    尝试{
        Log.v(TAG,下载数据);

        网址URL =新的URL(http://10.0.2.2/1.zip);
        URLConnection的连接= url.openConnection();
        connection.connect();

        INT lenghtOfFile = connection.getContentLength();

        Log.v(TAG,lenghtOfFile =+ lenghtOfFile);

        InputStream的是= url.openStream();

        文件testDirectory =新的文件(Environment.getExternalStorageDirectory()+/ testDirectory /);
    如果(!testDirectory.exists()){
        testDirectory.mkdir();
    }

        FileOutputStream中FOS =新的FileOutputStream(testDirectory +/ files.zip);

        字节的数据[] =新的字节[1024];

        诠释计数= 0;
        总长= 0;
        INT进度= 0;

        而((计数= is.​​read(数据))!=  -  1)
        {
            共有+ =计数;
            INT progress_temp =(int)的总* 100 / lenghtOfFile;
            如果(progress_temp%10 == 0安培;&安培;进步= progress_temp!){
                进度= progress_temp;
                Log.v(TAG,总=+进步);
            }
            fos.write(数据,0,计数);
        }

        is.close();
        fos.close();

    Log.v(TAG,下载完毕);

    }赶上(例外五){
        Log.v(TAG,例外downloadData);
        e.printStackTrace();
    }

}
 
使用Android Studio的AVD进行测试

任何线索? 非常感谢你。

更新与更多的日志说明:

  

嗨克里斯,我试图发现什么   起初打算与此日志中   Eclipse环境。这里有   点点的细节是什么   发生(时间 - 作用),注意   我已经改变了,下载的文件   另一位在orther做多   测试,但结果是相当的   相同的:

  00:00将开始下载:
V / iPhoto中(853):将数据下载
V / iPhoto中(853):lenghtOfFile = 7732809
V / iPhoto中(853):总= 10
V / iPhoto中(853):总= 20
V / iPhoto中(853):总= 30
V / iPhoto中(853):总= 40
V / iPhoto中(853):总= 50(圆00:05)
 - >在这里,它停止和DDMS立即断开装置

03:40它完成下载(不总是)与设备重启自己:
I /处理(595):发送信号。 PID:595 SIG:3
I / dalvikvm(595):主题ID = 7:反应信号3
D / dalvikvm(722):GC释放2193的对象/ 176秒135808字节
V / iPhoto中(853):总= 60
I / dalvikvm(595):写堆栈跟踪/data/anr/traces.txt
I / ActivityManager(595):过程com.google.android.apps.maps:FriendService(PID 778)已经死亡。
I / ActivityManager(595):过程com.android.mms(PID 732)已经死亡。
V / iPhoto中(853):总= 70
V / iPhoto中(853):总= 80
V / iPhoto中(853):总= 90
V / iPhoto中(853):总= 100
V / iPhoto中(853):下载完成
V / iPhoto中(853):线程完成加载
I /处理(595):发送信号。 PID:595 SIG:9
I / ActivityThread(757):去除死皮内容提供商:设置
I / ActivityThread(748):去除死皮内容提供商:设置
I / ActivityThread(722):去除死皮内容提供商:设置
I / ActivityThread(700):去除死皮内容提供商:设置
我/的ServiceManager(549):服务一揽子去世
...服务奄奄一息......
我/的ServiceManager(549):服务无线去世
E / INSTALLD(557):EOF
E / INSTALLD(557):无法读取尺寸
I / INSTALLD(557):关闭连接
D / qemud(560):fdhandler_event:断开的FD 11
D / qemud(560):fdhandler_event:断开的FD 12
E / vold的(550):框架断开
I /合子(554):退出合子,因为系统服务器(595)已终止
我/的ServiceManager(549):服务simphonebook'死亡
我/的ServiceManager(549):服务'主义'死亡
我/的ServiceManager(549):服务iphonesubinfo'死亡
我/的ServiceManager(549):服务电话死了
D / AndroidRuntime(949):
D / AndroidRuntime(949):>>>>>>>>>>>>>> AndroidRuntime START<<<<<<<<<<<<<<
...并开始...
 

任何人都可以尽我的code和下载大小为8MB圆一个zip文件?它是否适合你?

解决方案

我只是想这code在真实的设备和它完美的作品。必须有在模拟器奇怪的事情。

I'm trying to download a large .zip file from a web server but I have a weird behaviour, the description is:

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>

The error:

It starts downloading the file, I can see the text 10, 20, 30, 40, 50 and then it stops at 60. After a while, the emulator reboots itself.

Workarounds:

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();

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.

Update with more log description:

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 ...

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.

 
精彩推荐