Android Download network picture saved to local

Source: Internet
Author: User

Get the network image through the network address, click the download to display the picture, then click the picture to save the picture locally.you first need to add some permissions on the manifest:
[Java]View PlainCopy
    1. <!--permissions to access the network--
    2. <uses-permission android:name="Android.permission.INTERNET"/>
    3. <!--file Read permissions--
    4. <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/>
    5. <!--permissions created or removed from the SD card. -
    6. <uses-permission android:name="Andorid.permission.MONUN_UNMOUNT_FILESYSTEMS"/>
[Java]View PlainCopy
[Java]View PlainCopy
    1. Package com.xjw.test;
    2. Import Java.io.File;
    3. Import Java.io.FileOutputStream;
    4. Import Java.io.InputStream;
    5. Import java.net.HttpURLConnection;
    6. Import Java.net.URL;
    7. Import Android.os.AsyncTask;
    8. Import Android.os.Bundle;
    9. Import android.os.Environment;
    10. Import Android.os.Handler;
    11. Import Android.os.Message;
    12. Import android.app.Activity;
    13. Import Android.graphics.Bitmap;
    14. Import Android.graphics.BitmapFactory;
    15. Import Android.view.View;
    16. Import Android.view.View.OnClickListener;
    17. Import Android.widget.Button;
    18. Import Android.widget.ImageView;
    19. Import Android.widget.Toast;
    20. Public class Mainactivity extends Activity implements onclicklistener{
    21. Button content;
    22. ImageView image;
    23. Bitmap Bitmap;
    24. protected void OnCreate (Bundle savedinstancestate) {
    25. super.oncreate (savedinstancestate);
    26. Setcontentview (R.layout.activity_main);
    27. Content= (Button) Findviewbyid (r.id.content);
    28. Image= (ImageView) Findviewbyid (r.id.image);
    29. Content.setonclicklistener (this);;
    30. Image.setonclicklistener (this);;
    31. }
    32. /** 
    33. * Get network pictures
    34. * @param imageurl Picture Network address
    35. * @return Bitmap return bitmap
    36. */
    37. Public Bitmap Getimageinputstream (String imageurl) {
    38. URL url;
    39. HttpURLConnection connection=null;
    40. Bitmap bitmap=null;
    41. try {
    42. url = new URL (imageurl);
    43. connection= (HttpURLConnection) url.openconnection ();
    44. Connection.setconnecttimeout (6000); //Timeout settings
    45. Connection.setdoinput (true);
    46. Connection.setusecaches (false); //Set not to use cache
    47. InputStream Inputstream=connection.getinputstream ();
    48. Bitmap=bitmapfactory.decodestream (InputStream);
    49. Inputstream.close ();
    50. } catch (Exception e) {
    51. E.printstacktrace ();
    52. }
    53. return bitmap;
    54. }
    55. public void OnClick (View v) {
    56. switch (V.getid ()) {
    57. Case R.id.content:
    58. //Add Network image Address
    59. new Task (). Execute ("http://pic.4j4j.cn/upload/pic/20130617/55695c3c95.jpg");
    60. Break ;
    61. Case R.id.image:
    62. //Click the picture to save the picture to the test folder in the SD card and directory
    63. Savaimage (Bitmap, Environment.getexternalstoragedirectory (). GetPath () +"/test");
    64. Toast.maketext (Getbasecontext (), "Picture Save", Toast.length_short). Show ();
    65. Break ;
    66. Default:
    67. Break ;
    68. }
    69. }
    70. Handler handler=New Handler () {
    71. public void Handlemessage (Android.os.Message msg) {
    72. if (msg.what==0x123) {
    73. Image.setimagebitmap (bitmap);
    74. }
    75. };
    76. };
    77. /** 
    78. * Asynchronous thread Download picture
    79. *
    80. */
    81. class Task extends Asynctask<string, Integer, void>{
    82. protected Void doinbackground (String ... params) {
    83. Bitmap=getimageinputstream ((String) params[0]);
    84. return null;
    85. }
    86. protected void onpostexecute (void result) {
    87. Super.onpostexecute (Result);
    88. Message message=new Message ();
    89. message.what=0x123;
    90. Handler.sendmessage (message);
    91. }
    92. }
    93. /** 
    94. * Save Bitmap to Local
    95. * @param bitmap
    96. * @param path Local
    97. * @return void
    98. */
    99. public void Savaimage (Bitmap Bitmap, String path) {
    100. File file=new file (path);
    101. FileOutputStream fileoutputstream=null;
    102. //folder does not exist, it is created
    103. if (!file.exists ()) {
    104. File.mkdir ();
    105. }
    106. try {
    107. fileoutputstream=New FileOutputStream (path+"/" +system.currenttimemillis () +". png");
    108. Bitmap.compress (Bitmap.CompressFormat.JPEG, 100,fileoutputstream);
    109. Fileoutputstream.close ();
    110. } catch (Exception e) {
    111. E.printstacktrace ();
    112. }
    113. }
    114. }

Android Download network picture saved to local

Related Article

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.