android= "Download image

Source: Internet
Author: User

public class C6demo01_downimageactivity extends Activity {
/** called when the activity is first created. */
Private Button btn;
Private ImageView IV;
Private URL URL;
private InputStream input;
Private FileOutputStream output;
Private File path;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Btn= (Button) Findviewbyid (R.ID.BTN);
iv= (ImageView) Findviewbyid (R.ID.IV);
Btn.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
try {
Url=new URL ("http://wenwen.soso.com/p/20110724/20110724161153-1243191028.jpg");
Input=url.openstream ();
System.out.println ("input=" +input);
Bitmap bitmap=bitmapfactory.decodestream (input);
Iv.setimagebitmap (bitmap);

Input.close ();
// Writetosdcard ();
} catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}
});

}
public void Writetosdcard () {
Path=new File (environment.getexternalstoragedirectory () + "/images");
if (!path.exists ()) {
Path.mkdirs ();
}
File File=new file (path+ "/image.jpg");
try {
Output=new fileoutputstream (file);
Input=url.openstream ();
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

Byte[] Buffer=new byte[1024];
int t;
try {
while ((T=input.read (buffer))!=-1) {
Output.write (buffer,0,t);
Input.close ();
Output.close ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

}

android= "Download image

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.