Android Network picture Viewer

Source: Internet
Author: User

Network Picture Viewer
Enter the network picture path, click to view the Web app's pictures displayed on the phone
Mainactivity
{
EditText pathtext= (EditText) This.findviewbyid (R.id.imagepath);
Button button= (button) This.findviewbyid (R.id.button);
ImageView ImageView = (ImageView) This.findviewbyid (R.id.imageview);
Button.setonclicklistener (New Buttonclicklistener ());
}
private static class Buttonclicklistener implements View.onclicklistener
{
public void OnClick (View v)
{
String path = Pathtext.gettext (). toString ();
try{
byte[] data = imageservice.getimage (path); Get the picture after the image in a byte array to hold the picture data, the process of getting the picture for the business service
Bitmap Bitmap = Bitmapfactory.decodebytearray (data,0,data.length);
Imageview.setimagebitmap (bitmap); Show pictures
}catch (Exception e)
{
E.printstacktrace ();
Toast.maketext (Getapplicationcontext (), r.string.error,1). Show ();
}
}
}
/* Get network picture data */
public class ImageService
{
public static byte[] GetImage (String path) throws Exception
{
URL url = new URL (path);
HttpURLConnection conn= (httpurlconnection) url.openconnection (); Connection object based on HTTP protocol
Conn.setconnecttimeout (5000);
Conn.setrequestmethod ("GET");
if (conn.getresponsecode () = = 200)//Determine if the request was successful
InputStream instream = Conn.getinputstream ();
Return Streamtool.read (instream); Encapsulates a tool class that reads binary data from a stream
}
}

public class Streamtool
{
Reading data from a stream
public static byte[] Read (InputStream instream) throws Exception
{
Bytearrayoutputstream OutStream = new Bytearrayoutputstream ();
byte[] buffer = new byte[1024];
int len=0;
while (len = instream.read (buffer))!=-1)
{
Outstream.write (Buffer,0,len);
}
Instream.close ();
return Outstream.tobytearray ();
}
}

Server-side Dynamic Web Project
Store a picture and start the server. Http://ip:8080/web/gg.gif

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.