Android network_network Image Viewer,

Source: Internet
Author: User

Android network_network Image Viewer,

Xml

<? Xml version = "1.0"?> -<LinearLayout tools: context = ". mainActivity "android: paddingTop =" @ dimen/activity_vertical_margin "android: paddingRight =" @ dimen/activity_horizontal_margin "android: paddingLeft =" @ dimen/plugin "android: paddingBottom = "@ dimen/Docs" android: orientation = "vertical" android: layout_height = "match_parent" android: layout_width = "match_parent" xmlns: tools = "http://schemas.android.com/tools" xmlns: android = "http://schemas.android.com/apk/res/android"> <EditText android: id = "@ + id/et_url" android: layout_height = "wrap_content" android: layout_width = "fill_parent" android: text = "http://www.baidu.com"/> <Button android: id = "@ + id/bt_looksource" android: layout_height = "wrap_content" android: layout_width = "fill_parent" android: text = "view image"/>-<ScrollView android: layout_height = "wrap_content" android: layout_width = "wrap_content"> <ImageView android: id = "@ + id/img_pic" android: layout_height = "match_parent" android: layout_width = "match_parent"/> </ScrollView> </LinearLayout>Image Display Control

 

Java

 

Package com. itheima. sourcelook; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; import java.net. URLConnection; import android. OS. bundle; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. app. activity; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. text. textUtils; Import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. imageView; import android. widget. textView; import android. widget. toast; import com. itheima. piclook. r; public class MainActivity extends Activity implements OnClickListener {private EditText et_url; private ImageView img_pic; priva Te Context mContext; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mContext = this; et_url = (EditText) findViewById (R. id. et_url); Button bt_looksource = (Button) findViewById (R. id. bt_looksource); img_pic = (ImageView) findViewById (R. id. img_pic); // 2. set the Click Event bt_looksource.setOnClickListener (this); System. out. Println ("oncreate method Thread:" + Thread. currentThread (). getName ();} // ☆☆☆1. create a Handler object private Handler handler = new Handler () {// ☆☆☆2. override the handlermessage method of handler to receive the public void handleMessage (android. OS. message msg) {// ☆☆☆5. receives data sent by the subthread and processes the data. Bitmap bitmap = (Bitmap) msg. obj; // ☆☆☆6. the current method belongs to the main thread which can be used to update the UI // 5. obtain the content returned by the server and display it on the textview img_pic.setImageBitmap (bitmap); // set the image content of the ImageView };}; @ Override public void onClick (View v) {try {// 3. in the oclick method, obtain the url entered by the user. final String url_str = et_url.getText (). toString (). trim (); if (TextUtils. isEmpty (url_str) {Toast. makeText (mContext, "url cannot be blank", 0 ). show (); return;} System. out. println ("oclick method thread:" + Thre Ad. currentThread (). getName (); // create a subthread for network requests new Thread (new Runnable () {@ Override public void run () {try {System. out. println ("oclick method runnable Thread:" + Thread. currentThread (). getName (); // 4. request url // 1. create a Url object URL = new url (url_str); // 2. obtain the url of the UrlConnection object HttpURLConnection connection = (HttpURLConnection. openConnection (); // 3. set Request Parameters, request methods, and connection timeout for the UrlConnection object. setRequestM Ethod ("GET"); // sets the request method connection. setConnectTimeout (1000*10); // set the timeout time // 4. before obtaining the data of a url request, you need to determine the response code: 200: Successful, 206: access part of the data is successful. 300: jump or redirect: 400: Error 500: Server exception int code = connection. getResponseCode (); if (code = 200) {// 5. obtain valid data and parse the obtained stream data to String InputStream inputStream = connection. getInputStream (); // converts a read stream into an image Drawable, Btimap: bitmap ????? Bitmap bitmap = BitmapFactory. decodeStream (inputStream); // ☆☆☆3. Create a Message object in the subthread to carry the data obtained in the subthread to the main thread. Message msg = Message. obtain (); // gets a Message object. The internal implementation is: if the previous Message exists, it will be returned directly. If no new Message is created, msg will be returned. obj = bitmap; // encapsulate the obtained data into msg. // ☆☆☆4. Use the handler object to send the message to the main thread. Handler. sendMessage (msg) ;}} catch (Exception e) {e. printStackTrace ();}}}). start ();} catch (Exception e) {e. printStackTrace ();}}}MainActivity

 

Byte stream operations

Package com. itheima. sourcelook; import java. io. byteArrayOutputStream; import java. io. inputStream; public class StreamUtils {public static String streamToString (InputStream in) {String result = ""; try {// create a byte array to write the stream ByteArrayOutputStream out = new ByteArrayOutputStream (); byte [] buffer = new byte [1024]; int length = 0; while (length = in. read (buffer ))! =-1) {out. write (buffer, 0, length); out. flush ();} result = out. toString (); // convert byte streams to string out. close ();} catch (Exception e) {e. printStackTrace ();} return result ;}}StreamUtils

 

Instructor notes

04 network image Viewer


Adb shell + input text content; you can input the content to the input box on the mobile phone.

Replace a read flow with a bitmap object:

BitmapFactory: converts a file, read stream, and byte array into a Bitmap object.
Bitmap bitmap = BitmapFactory. decodeStream (InputStream in );

ImageView. setImageBitmap (bitmap); // sets the image content

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.