Androidj basics: Download images from the Internet,

Source: Internet
Author: User

Androidj basics: Download images from the Internet,

I. layout files

On the settings page, add an ImageView and two Button to set its attributes and id.

<ImageView android: id = "@ + id/ImageView" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: background = "# ffffff"/> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_alignParentBottom = "true"> <Button android: id = "@ + id/bt1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "Download image"/> <Button android: id = "@ + id/bt2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: layout_toRightOf = "@ + id/bt1" android: text = "read image"/>

2. Declare network Permissions

Because you need to add permissions to use the network function, modify the AndroidManifest. xml file to add permission declaration statements.

 <uses-permission android:name="android.permission.INTERNET"/>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.administrator.httpexe">    <uses-permission android:name="android.permission.INTERNET"/>

</manifest>

3. Edit the ManActivity. java file.

Import... // The import Statement is automatically generated.
Public class MainActivity extends AppCompatActivity {ImageView imageView; Button button Button; Button button1; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imageView = (ImageView) findViewById (R. id. imageView); button = (Button) findViewById (R. id. bt1); button1 = (Button) findViewById (R. id. bt2); button. setOnClickLis Tener (new View. onClickListener () {@ Override public void onClick (View v) {MyTask task = new MyTask (); // create an asynchronous task object task.exe cute ("https://www.baidu.com/img/bd_logo1.png "); // create download task}); button1.setOnClickListener (new View. onClickListener () {// read image @ Override public void onClick (View v) {Bitmap bmp = null; try {FileInputStream FS = openFileInput ("a.png"); bmp = BitmapFactory. decodeStream (FCM); if (bmp! = Null) {imageView. setImageBitmap (bmp) ;}} catch (Exception ex) {}});} public class MyTask extends AsyncTask <String, Void, Bitmap >{// bmp Bitmap, generic @ Override protected Bitmap doInBackground (String... params) {// download String urlStr = params [0]; // obtain the first variable parameter Bitmap bmp = null; try {URL url = new URL (urlStr ); bmp = BitmapFactory. decodeStream (url. openStream (); // bitmap factory, returns bitmap InputStream is = url. openStre Am (); byte [] buffer = new byte [4096]; // byte [] buffer FileOutputStream fos = MainActivity. this. openFileOutput ("a.png", MainActivity. MODE_PRIVATE); int hasRead = is. read (buffer); while (hasRead> 0) {fos. write (buffer, 0, hasRead); // file size, starting position, read Amount hasRead = is. read (buffer);} fos. close (); // close is. close (); // close} catch (Exception ex) {} finally {} return bmp;} @ Override protected void onPostExecute (Bitmap bitmap) {I F (bitmap! = Null) {imageView. setImageBitmap (bitmap );}}}

 

Iv. Running results

Click Download to download the image. Function of the read image button: After a download, you do not need to download the image again..

Note:

When the java code is entered, some of them will automatically pop up and match the corresponding package, and an error will be reported directly.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.