File Download....gehts auch einfacher, eleganter?

S

Sico

Erfahrenes Mitglied
51
Hallo,

ich habe hier mal ein bischen Code zum Thema DateiDownload mit Progressbar...

Meine Frage, kann man das noch einfacher, eleganter lösen?

Code:
package de.download;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.TextView;


public class download extends Activity {
    
    
    clockThread thread;
    TextView tv1,tv2;
    ProgressBar pg1;
    long numWritten = 0;
    int contentLength;

        @Override
        public void onCreate(Bundle icicle) {

                super.onCreate(icicle);
                setContentView(R.layout.main);
                tv1 = (TextView) findViewById(R.id.TextView01);
                tv2 = (TextView) findViewById(R.id.TextView02);
                pg1 = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
                thread = new clockThread();
                
                    thread.start();
                

                }
        
        private class clockThread extends Thread {
            @Override public void run() {
               

        DownloadFromUrl("http://cyanogen-updater.googlecode.com/files/update-cm-5.0.7-DS-test3-signed.zip","/sdcard/test.zip");
            }}
                
        private Handler showAdressResults = new Handler() {
              public void handleMessage(Message msg) {
                  pg1.setMax(contentLength);
                  pg1.setProgress((int)numWritten);
                  tv2.setText("Downloadgröße: "+new Integer(contentLength).toString());  
                tv1.setText("Downloade: "+new Long(numWritten).toString());  
              }
            
        };
        
        
        public void DownloadFromUrl(String DURL, String fileName) {  
                
                    
                    
                    try {
                        
                        String urlStr = DURL;
                        URL url = new URL(urlStr);
                        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
                        contentLength = httpConn.getContentLength();
                        
                        
                        OutputStream out = new BufferedOutputStream(
                        
                        new FileOutputStream(fileName));
                        InputStream inStream = httpConn.getInputStream();
                        
                        byte[] buffer = new byte[1024];
                        
                        int numRead;
                        
                        
                        
                        while ((numRead = inStream.read(buffer)) != -1) {
                        
                        out.write(buffer, 0, numRead);
                        
                        numWritten += numRead;
                        showAdressResults.sendEmptyMessage(0);
                        }
                        
                        
                        httpConn.disconnect();
                        
                        
                       

                    } catch (MalformedURLException e) {
                        
                        e.printStackTrace();
                    } catch (IOException e) {
                        
                        e.printStackTrace();
                    }
                    
                
                 
                
        }

        
}
 

Ähnliche Themen

wernho
Antworten
11
Aufrufe
606
wernho
wernho
M
  • myoggradio
Antworten
1
Aufrufe
760
myoggradio
M
B
Antworten
6
Aufrufe
1.016
jogimuc
J
Zurück
Oben Unten