Network Image Viewer
Add the following network access permissions to the configuration file:
View plain
| <! -- Access internet permissions -->
<Uses-permission android: name = "android. permission. INTERNET"/>
The interface is as follows:
Example:
View plain
<Span style = "FONT-WEIGHT: normal"> 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); // obtain image data
If (data! = Null ){
// Construct a bitmap object
Bitmap bitmap = BitmapFactory. decodeByteArray (data, 0, data. length );
ImageView. setImageBitmap (bitmap); // display the image
} Else {
Toast. makeText (getApplicationContext (), R. string. error, 1). show ();
}
} Catch (Exception e ){
Toast. makeText (getApplicationContext (), R. string. error, 1). show ();
}
}
});
}
} </Span>
View plain
<Span style = "FONT-WEIGHT: normal"> public class ImageService {
/**
* Getting Images
* @ Param path: network image path
* @ Return byte data of the image
*/
Public static byte [] getImage (String path) throws Exception {
URL url = new URL (path );
HttpURLConnection conn = (HttpURLConnection) url. openConnection ();
// Set the timeout value
Conn. setConnectTimeout (5000 );
Conn. setRequestMethod ("GET ");
If (conn. getResponseCode () = 200 ){
InputStream inStream = conn. getInputStream ();
Byte [] data = StreamTool. read (inStream );
Return data;
}
Return null;
}
} </Span>
View plain
<Span style = "FONT-WEIGHT: normal"> 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 ();
}
} </Span>
Webpage source code Viewer
If the source code of the webpage exceeds the display position of the screen, a scroll bar is required.
View plain
<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
View plain
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
PathText = (EditText) this. findViewById (R. id. path );
Htmlsource = (TextView) this.findViewById(R.id.html source );
Button button = (Button) this. findViewById (R. id. button );
Button. setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
String path = pathText. getText (). toString ();
Try {
// Obtain the source code
String html = PageService. getHtml (path );
Htmlsource. setText (html );
} Catch (Exception e ){
Toast. makeText (getApplicationContext (), R. string. error, 1). show ();
}
}
});
}
View plain
Public class PageService {
/**
* Obtain the webpage source code
* @ Param path: webpage path
* @ Return
*/
Public static String getHtml (String path) throws Exception {
URL url = new URL (path );
HttpURLConnection conn = (HttpURLConnection) url. openConnection ();
Conn. setConnectTimeout (5000 );
Conn. setRequestMethod ("GET ");
If (conn. getResponseCode () = 200 ){
Byte [] data = StreamTool. read (conn. getInputStream ());
Return new String (data, "UTF-8 ");
}
Return null;
}
}
Author: Fu rongkang Column"