package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class main extends Activity {
/** Called when the activity is first created. */
ProgressBar pb;
TextView tv;
int fileSize;
int downLoadFileSize;
String fileEx,fileNa,filename;
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{//Define a Handler to handle communication between the download thread and the UI
if (!().isInterrupted())
{
switch ()
{
case 0:
(fileSize);
case 1:
(downLoadFileSize);
int result = downLoadFileSize * 100 / fileSize;
(result + "%");
break;
case 2:
(, "File download is completed", 1).show();
break;
case -1:
String error = ().getString("error");
(, error, 1).show();
break;
}
}
(msg);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
pb=(ProgressBar)findViewById(.down_pb);
tv=(TextView)findViewById();
new Thread(){
public void run(){
try {
down_file("/upload/1/bigImage/","/sdcard/");
//Download file, parameters: first URL, second storage path
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
();
} catch (IOException e) {
// TODO Auto-generated catch block
();
}
}
}.start();
}
public void down_file(String url,String path) throws IOException{
//Download function
filename=(("/") + 1);
//Get file name
URL myURL = new URL(url);
URLConnection conn = ();
();
InputStream is = ();
= ();//Get file size based on the response
if ( <= 0) throw new RuntimeException("File size cannot be known");
if (is == null) throw new RuntimeException("stream is null");
FileOutputStream fos = new FileOutputStream(path+filename);
//Save data into path + file name
byte buf[] = new byte[1024];
downLoadFileSize = 0;
sendMsg(0);
do
{
//Loop reading
int numread = (buf);
if (numread == -1)
{
break;
}
(buf, 0, numread);
downLoadFileSize += numread;
sendMsg(1);//Update progress bar
} while (true);
sendMsg(2);//Notification download is completed
try
{
();
} catch (Exception ex)
{
("tag", "error: " + (), ex);
}
}
private void sendMsg(int flag)
{
Message msg = new Message();
= flag;
(msg);
}
}