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 ();}}}