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