【问题标题】:Android download large files安卓下载大文件
【发布时间】:2010-10-27 09:42:31
【问题描述】:

我正在尝试从网络服务器下载一个大的 .zip 文件,但我有一个奇怪的行为,描述是:

  • 我正在使用 512MB 的 SD 卡在设备模拟器 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 管理器 = (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, 0, count);
        }

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

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

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

}

有什么线索吗? 非常感谢。

更新更多日志描述:

嗨,克里斯,我试图发现什么 起初是通过这个登录进行的 Eclipse 环境。这里有一个 关于什么是更多细节 发生(时间 - 动作),请注意 我已更改要下载的文件 另一个做更多的事情 测试,但结果相当 一样的:

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 文件吗?对你有用吗?

【问题讨论】:

  • 感谢这段代码,第一次在我的三星 S3 上完美运行。

标签: android download


【解决方案1】:

我刚刚在真实设备中尝试过这段代码,它运行良好。模拟器里肯定有什么奇怪的地方。

【讨论】:

  • 嗨,我也有同样的问题。您是否设法使其在模拟器上运行?
  • 您是否在 Linux 上使用最新的 SDK 和 AVD?在 AVD 管理器左侧的第二个选项卡中,选择“Installed packages”和“Update all”,它会告诉你是否有新版本。
【解决方案2】:

“模拟器自动重启”听起来不太好。我建议你在开发机器上启动 logcat 并记录到一个文件中......即,在类似

的 linux 开发机器上

adb logcat |三通日志文件

然后查看日志文件,看看您可以了解它为何重新启动。

【讨论】:

  • 嗨,克里斯,有关此日志的原始问题的更多详细信息。
猜你喜欢
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
  • 2017-07-19
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
相关资源
最近更新 更多