Android télécharger de gros fichiers

Je suis en train de télécharger un grand .fichier zip à partir d'un serveur web, mais j'ai un comportement bizarre, la description est:

  • J'exécute le code dans l'émulateur de périphérique, l'API de niveau 3 (version 1.5) avec une carte SD de 512 MO. J'ai démarrer l'appareil avec "effacer les données de l'utilisateur"
  • La longueur de la taille à partir de conexion.getContentLength() est 7012725
  • L'adresse du serveur est localhost (10.0.2.2), mais j'ai essayé avec un serveur externe, et le comportement est le même. J'ai vérifié que je peux télécharger le fichier via un navigateur web.
  • J'ai ces autorisations dans le fichier manifeste:

-

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

L'erreur:

  • Il commence à télécharger le fichier, je peux voir le texte 10, 20, 30, 40, 50 et ensuite il s'arrête à 60.
  • Après un certain temps, l'émulateur redémarre de lui-même.

Solutions de contournement:

  • Cette problème dans stackoverflow est de travailler dans l'appareil, pas dans mon cas.
  • Sur ce wifi de verrouillage question ce que j'ai fait est d'ajouter cette autorisation "d'android.la permission.WAKE_LOCK" et puis ce morceau de code, mais avec exactement le même comportement:

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

Voici le code, qu'il soit exécuté dans un thread séparé:

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

}

La moindre idée?
Je vous remercie beaucoup.

Mise à jour avec plus de journal description:

Salut Chris, j'ai essayé de découvrir ce qui
qui se passait dans un premier temps avec ce journal dans
l'environnement Eclipse. Voici un
peu peu plus de détails sur ce qui est
passe (le temps de l'action), note que
J'ai modifié le fichier à télécharger
un autre l'un dans l'autre pour ne plus
tests, mais les résultats sont tout à fait la
même:

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

Quelqu'un pourrait-il essayer mon code et pour télécharger un fichier zip avec la taille ronde 8MO? Ça fonctionne pour vous?

source d'informationauteur zegnus