This article describes the Android programming implementation of Network Image Viewer and Web page Source viewer. Share to everyone for your reference, specific as follows:
Network Picture Viewer
Manifest to join the network access rights:
<!--access to INTERNET permissions-->
<uses-permission android:name= "Android.permission.INTERNET"/>
The interface is as follows:
Example:
public class Mainactivity extends activity {private EditText ImagePath;
Private ImageView ImageView;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
ImagePath = (edittext) This.findviewbyid (R.id.imagepath);
ImageView = (ImageView) This.findviewbyid (R.id.imageview);
Button button = (button) This.findviewbyid (R.id.button); Button.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {String path = ImagePath
. GetText (). toString ();
try{byte[] data = imageservice.getimage (path);//Get Picture data if (data!=null) {//Build Bitmap Object
Bitmap Bitmap = bitmapfactory.decodebytearray (data, 0, data.length); Imageview.setimagebitmap (bitmap)//Display picture}else{Toast.maketext (Getapplicationcontext (), R.string.erro
R, 1). Show (); }}catch (Exception e) {Toast.maketext (gEtapplicationcontext (), R.string.error, 1). Show ();
}
}
});
}
}
public class ImageService {/** * get picture * @param path network picture paths * @return The byte data of a picture
*/public static byte[] GetImage (String path) throws exception{url url = new URL (path);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Set timeout time conn.setconnecttimeout (5000);
Conn.setrequestmethod ("get");
if (Conn.getresponsecode () ==200) {InputStream instream = Conn.getinputstream ();
byte[] data = Streamtool.read (instream);
return data;
return null; }
}
public class Streamtool {
/**
* Read input stream data
* @param instream
* @return * * 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 ();
}
Web page Source Viewer
Scroll bars are required if the source of the page exceeds the display position of the screen.
<scrollview
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
>
<textview
android:layout_width= "fill_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/htmlsource"
/>
</ScrollView>
The interface is as follows:
Example
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (Savedinsta
Ncestate);
Setcontentview (R.layout.main);
Pathtext = (edittext) This.findviewbyid (R.id.path);
Htmlsource = (TextView) This.findviewbyid (R.id.htmlsource);
Button button = (button) This.findviewbyid (R.id.button); Button.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {String path = Pathtext.gettext (
). ToString ();
try{//Get source code String HTML = pageservice.gethtml (path);
Htmlsource.settext (HTML);
}catch (Exception e) {toast.maketext (Getapplicationcontext (), R.string.error, 1). Show ();
}
}
}); }
public class Pageservice {
/**
* Fetch Web page source code
* @param path Web page Paths
* @return/public
static String get Html (String path) throws exception{
url url = new URL (path);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setconnecttimeout (5000);
Conn.setrequestmethod ("get");
if (conn.getresponsecode () = =) {
byte[] data = Streamtool.read (Conn.getinputstream ());
return new String (data, "UTF-8");
}
return null;
}
}
I hope this article will help you with the Android program.