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
- <!--permissions to access the network--
- <uses-permission android:name="Android.permission.INTERNET"/>
- <!--file Read permissions--
- <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/>
- <!--permissions created or removed from the SD card. -
- <uses-permission android:name="Andorid.permission.MONUN_UNMOUNT_FILESYSTEMS"/>
[Java]View PlainCopy
[Java]View PlainCopy
- Package com.xjw.test;
- Import Java.io.File;
- Import Java.io.FileOutputStream;
- Import Java.io.InputStream;
- Import java.net.HttpURLConnection;
- Import Java.net.URL;
- Import Android.os.AsyncTask;
- Import Android.os.Bundle;
- Import android.os.Environment;
- Import Android.os.Handler;
- Import Android.os.Message;
- Import android.app.Activity;
- Import Android.graphics.Bitmap;
- Import Android.graphics.BitmapFactory;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.widget.Button;
- Import Android.widget.ImageView;
- Import Android.widget.Toast;
- Public class Mainactivity extends Activity implements onclicklistener{
- Button content;
- ImageView image;
- Bitmap Bitmap;
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Content= (Button) Findviewbyid (r.id.content);
- Image= (ImageView) Findviewbyid (r.id.image);
- Content.setonclicklistener (this);;
- Image.setonclicklistener (this);;
- }
- /**
- * Get network pictures
- * @param imageurl Picture Network address
- * @return Bitmap return bitmap
- */
- Public Bitmap Getimageinputstream (String imageurl) {
- URL url;
- HttpURLConnection connection=null;
- Bitmap bitmap=null;
- try {
- url = new URL (imageurl);
- connection= (HttpURLConnection) url.openconnection ();
- Connection.setconnecttimeout (6000); //Timeout settings
- Connection.setdoinput (true);
- Connection.setusecaches (false); //Set not to use cache
- InputStream Inputstream=connection.getinputstream ();
- Bitmap=bitmapfactory.decodestream (InputStream);
- Inputstream.close ();
- } catch (Exception e) {
- E.printstacktrace ();
- }
- return bitmap;
- }
- public void OnClick (View v) {
- switch (V.getid ()) {
- Case R.id.content:
- //Add Network image Address
- new Task (). Execute ("http://pic.4j4j.cn/upload/pic/20130617/55695c3c95.jpg");
- Break ;
- Case R.id.image:
- //Click the picture to save the picture to the test folder in the SD card and directory
- Savaimage (Bitmap, Environment.getexternalstoragedirectory (). GetPath () +"/test");
- Toast.maketext (Getbasecontext (), "Picture Save", Toast.length_short). Show ();
- Break ;
- Default:
- Break ;
- }
- }
- Handler handler=New Handler () {
- public void Handlemessage (Android.os.Message msg) {
- if (msg.what==0x123) {
- Image.setimagebitmap (bitmap);
- }
- };
- };
- /**
- * Asynchronous thread Download picture
- *
- */
- class Task extends Asynctask<string, Integer, void>{
- protected Void doinbackground (String ... params) {
- Bitmap=getimageinputstream ((String) params[0]);
- return null;
- }
- protected void onpostexecute (void result) {
- Super.onpostexecute (Result);
- Message message=new Message ();
- message.what=0x123;
- Handler.sendmessage (message);
- }
- }
- /**
- * Save Bitmap to Local
- * @param bitmap
- * @param path Local
- * @return void
- */
- public void Savaimage (Bitmap Bitmap, String path) {
- File file=new file (path);
- FileOutputStream fileoutputstream=null;
- //folder does not exist, it is created
- if (!file.exists ()) {
- File.mkdir ();
- }
- try {
- fileoutputstream=New FileOutputStream (path+"/" +system.currenttimemillis () +". png");
- Bitmap.compress (Bitmap.CompressFormat.JPEG, 100,fileoutputstream);
- Fileoutputstream.close ();
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- }
Android Download network picture saved to local