Android implements network image viewer and webpage source code Viewer

Source: Internet
Author: User

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"
 

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.