微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android下载大文件

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

>我正在使用512MB的SD卡在设备模拟器API API 3(版本1.5)中执行代码.我用“擦除数据用户”启动设备
> conexion.getContentLength()的大小长度为7012725
>服务器地址是localhost(10.0.2.2),但我尝试使用外部服务器,行为是一样的.我已经检查过,我可以通过网络浏览器下载文件.
>我在清单文件中有这些权限:

<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.
>一段时间后,仿真器重新启动.

解决方法

> This问题在stackoverflow是在设备中工作,而不是在我的情况.
>关于这个wifi锁issue我所做的是添加这个权限“android.permission.WAKE_LOCK”然后这段代码,但与完全相同的行为:

WifiManager.WifiLock wifilock;
WifiManager manager =(WifiManager)getSystemService(Context.WIFI_SERVICE);
wifilock = manager.createWifiLock(“wifilock”);
wifilock.acquire();

wifilock.release();

这是代码,它正在单独的线程中执行:

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,count);
        }

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

    Log.v(TAG,"downloading finished");

    }catch(Exception e){
        Log.v(TAG,"exception in downloadData");
        e.printstacktrace();
    }

}

任何线索?
非常感谢你.

更新更多的日志描述:

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

有没有人可以尝试我的代码,并下载大小为8MB的zip文件?它适用于你吗?

解决方法

我刚刚在一个真实的设备中尝试过这个代码,它的工作原理很好.仿真器中必须有奇怪的东西.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方法有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android岛屿数量算...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Andro...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“Android数据压缩的方法是什么”文章能帮助大家解决疑惑...
这篇“Android怎么使用Intent传大数据”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅...
本文小编为大家详细介绍“Android事件冲突怎么解决悬浮窗拖拽问题”,内容详细,步骤清晰,细节处理妥当,希望这篇“Android事件冲突怎么解决悬浮窗拖拽问题”文...
这篇文章主要介绍了Android拼接如何实现动态对象的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android拼接如何实现动态对象文...
今天小编给大家分享一下Android全面屏适配怎么实现的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下...