Android programming to implement network Picture Viewer and Web page Source Viewer instance _android

Source: Internet
Author: User
Tags gettext

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.

Related Article

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.