Here we will summarize how to use the android simulator to create an sdcard and learn how to download it in Android.
An online file is saved to Android and a progress bar is displayed during download.
1. The page structure provides a button. When a button is clicked, download an image. The main. XML design is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<Button
Android: text = "Start long running task .."
Android: Id = "@ + ID/startbtn"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
</Button>
</Linearlayout>
2 main program
Package com. Liao;
Import java. Io. bufferedinputstream;
Import java. Io. fileoutputstream;
Import java. Io. inputstream;
Import java. Io. outputstream;
Import java.net. url;
Import java.net. urlconnection;
Import Android. App. activity;
Import Android. App. Dialog;
Import Android. App. progressdialog;
Import Android. OS. asynctask;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Public class androasync extends activity {
Public static final int dialog_download_progress = 0;
Private button startbtn;
Private progressdialog mprogressdialog;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Startbtn = (button) findviewbyid (R. Id. startbtn );
Startbtn. setonclicklistener (New onclicklistener (){
Public void onclick (view v ){
Startdownload ();
}
});
}
Private void startdownload (){
String url = "http: // image-
7.verycd.com/0d7596f5db7df1742236466dc91311b1435702 (120x120)/thumb.jpg ";
New downloadfileasync(cmd.exe cute (URL );
}
@ Override
Protected dialog oncreatedialog (int id ){
Switch (ID ){
Case dialog_download_progress:
Mprogressdialog = new progressdialog (this );
Mprogressdialog. setmessage ("downloading file ..");
Mprogressdialog. setprogressstyle
(Progressdialog. style_horizontal );
Mprogressdialog. setcancelable (false );
Mprogressdialog. Show ();
Return mprogressdialog;
Default:
Return NULL;
}
}
Class downloadfileasync extends asynctask <string, String, string> {
@ Override
Protected void onpreexecute (){
Super. onpreexecute ();
Showdialog (dialog_download_progress );
}
@ Override
Protected string doinbackground (string... aurl ){
Int count;
Try {
URL url = new URL (aurl [0]);
Urlconnection conexion = URL. openconnection ();
Conexion. Connect ();
Int lenghtoffile = conexion. getcontentlength ();
Log. D ("andro_async", "lenght of file:" + lenghtoffile );
Inputstream input = new bufferedinputstream (URL. openstream ());
Outputstream output = new fileoutputstream
("/Sdcard/picture.jpg ");
Byte data [] = new byte [1024];
Long Total = 0;
While (COUNT = input. Read (data ))! =-1 ){
Total + = count;
Publishprogress ("" + (INT) (total * 100)/lenghtoffile ));
Output. Write (data, 0, count );
}
Output. Flush ();
Output. Close ();
Input. Close ();
} Catch (exception e ){
Log. E ("error", E. getmessage (). tostring ());
System. Out. println (E. getmessage (). tostring ());
}
Return NULL;
}
Protected void onprogressupdate (string... progress ){
Log. D ("andro_async", progress [0]);
Mprogressdialog. setprogress (integer. parseint (Progress [0]);
}
@ Override
Protected void onpostexecute (string unused ){
Dismissdialog (dialog_download_progress );
}
}
}
Note that the internal class downloadfileasync is called in the startdownload method.
It is convenient to use asynctask's asynchronous working features. Pay attention to doinbackground (in this phase
), Onprogressupdate is to make the progress bar disappear after completion.
Finally, remember to add </Application> In androidmanifest. xml
<Uses-Permission Android: Name = "android. Permission. Internet"/>
Allow access to the Internet
3. Next I will explain how to use it and save the image to sdcard. Because it is a simulator
Only one imgfile can be simulated on the disk. The steps are as follows:
1) Open cmd, go to the tools directory under the android SDK installation path, and enter the following command:
Mksdcard 1024 M sdcard. img
This command will generate an sdcard. imgfile in the current directory, which is the SD card image file of the android simulator.
. 1024m indicates that the SD card has a size of 1024m, that is, 1 GB. Android currently supports 8 m ~ Gb sd card.
2) run the simulator with sdcard
A. Start the simulator with sdcard in cmd.
Go to the tools directory under the android SDK installation path and run the following command:
Emulator-AVD name_avd-sdcard. img
Here, name_avd is the name of an existing simulator, and sdcard. IMG is the SD card image file created in step 1.
B. In eclipse, when creating a simulator, specify the path of the SD card image file and add support options for the SD card.
Open eclipse and go to window> Android SDK and AVD manager,
Then, when creating AVD, select "file" in SD card, and then select SD support;
3) view sdcard files
1) In cmd, go to the tools directory under the android SDK installation path and run the following command:
ADB push E: \ abc.jpg sdcard/abc.jpg
E: \ test.3gp is the local file to be uploaded, and sdcard/abc.jpg is the path uploaded to the SD card.
Note: ADB push computer path simulator path
"\" Must be used in the computer path, "/" must be used in the simulator path, and the root path of the simulator is
Read.
2) In the eclipse Environment
After setting the run command parameters, run an application and use the file explorer tool of ddms to import and export files.
. Open the ddms tool and open the ddms tool in eclipse Windows> open perspective> other ....
Select the sdcard directory in the file explorer label of the ddms tool to import and export files,