Save the network image to the SD card

Source: Internet
Author: User
Package com. example. saveimagetosdcard; import java. io. file; import java. io. fileOutputStream; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; import android. OS. bundle; import android. OS. environment; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmap. compressFormat; import android. graphics. bitmapFactory; import android. view. menu; // 1 Note permission Limit // 2 is only for testing. Therefore, this method is written to the public class MainActivity extends Activity {@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); this. getImageFromNetSaveToSDCard () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. activity_main, menu); return true;} public void getImageFromNetSaveT OSDCard () {try {Bitmap bitmap = null; InputStream inputStream = null; String imagePathString = "http://photocdn.sohu.com/20121109/Img357146253.jpg"; URL imageUrl = new URL (imagePathString); HttpURLConnection httpURLConnection = (HttpURLConnection) imageUrl. openConnection (); httpURLConnection. setConnectTimeout (5000); // httpURLConnection. setRequestMethod ("POST"); // set the image size obtained by POST to 0httpURLConnection. setReq UestMethod ("GET"); if (httpURLConnection. getResponseCode () == 200) {inputStream = httpURLConnection. getInputStream (); // use InputStream to generate bitmapbitmap = BitmapFactory. decodeStream (inputStream);} if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {// create a folder under the SD card String sdCardPath = Environment. getExternalStorageDirectory (). toString (); String folderName = "/haha"; String imageFolder Path = sdCardPath + folderName; File imageFolder = new File (imageFolderPath); if (! ImageFolder. exists () {imageFolder. mkdirs ();} // Save the image String everyImagedetailPath = imageFolder in the folder under the SD card. toString () + "/man.jpg"; File everyImageFile = new File (everyImagedetailPath); if (everyImageFile. createNewFile () {// FileOutputStream outputStream = new FileOutputStream (everyImageFile); bitmap. compress (CompressFormat. JPEG, 80, outputStream); outputStream. flush (); outputStream. close ();} else {System. out. println ("failed to save the image") ;}} catch (Exception e) {e. printStackTrace ();}}}

 

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.