Android client single-thread download, android client single-Thread

Source: Internet
Author: User

Android client single-thread download, android client single-Thread

The specific effect is shown in. The specific operation steps are as follows:

1. Create an application named getDataInternet;

2. Modify the string. xml file in res/values. The Code is as follows:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <resources> 3 4 <string name = "app_name"> Android client single-threaded download </string> 5 <string name = "hello_world"> Hello world! </String> 6 <string name = "action_settings"> Settings </string> 7 <string name = "path"> image path </string> 8 <string name = "getBtnValue ""> get image </string> 9 10 </resources>

3. Compile the fragment_main.xml file in res/Layout. The specific code is as follows:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:paddingBottom="@dimen/activity_vertical_margin" 6     android:paddingLeft="@dimen/activity_horizontal_margin" 7     android:paddingRight="@dimen/activity_horizontal_margin" 8     android:paddingTop="@dimen/activity_vertical_margin" 9     tools:context="com.example.getdatatointernet.MainActivity$PlaceholderFragment" >10 11     <LinearLayout 12         android:layout_width="fill_parent"13         android:layout_height="fill_parent"14         android:orientation="vertical"15         >16         <TextView 17             android:layout_width="fill_parent"18             android:layout_height="wrap_content"19             android:text="@string/path"20             />21         <EditText 22             android:layout_width="fill_parent"23             android:layout_height="wrap_content"24             android:text="http://b.hiphotos.baidu.com/image/pic/item/1ad5ad6eddc451da70d0dc8db4fd5266d01632b6.jpg"25             android:id="@+id/path"26             />27         <Button 28             android:layout_width="wrap_content"29             android:layout_height="wrap_content"30             android:text="@string/getBtnValue"31             android:id="@+id/sendBtn"32             />33         <ImageView 34             android:layout_width="wrap_content"35             android:layout_height="wrap_content"36             android:id="@+id/imageView"37             />38     </LinearLayout>39 40 </RelativeLayout>

4. Compile the NetTool. java class, which is used to download images. The Code is as follows:

1 package com. example. getdatatointernet; 2 3 import java. io. byteArrayOutputStream; 4 import java. io. inputStream; 5 import java.net. httpURLConnection; 6 import java.net. URL; 7 8 public class NetTool {9 10 public static byte [] getImage (String path) throws Exception {11 // resume URL path 12 URL url = new URL (path ); 13 14 // get the HttpURLConnection object 15 HttpURLConnection conn = (HttpURLConnection) url. openConnection (); 16 c Onn. setRequestMethod ("GET"); // declares the Request Method 17 conn. setConnectTimeout (6*1000); // set link timeout 18 int a = conn. getResponseCode (); 19 if (conn. getResponseCode () == 200) 20 {21 InputStream iStream = conn. getInputStream (); // get the input stream 22 return readInstream (iStream); // return the resulting input stream 23} 24 25 return null; 26} 27 28 // read file stream content 29 public static byte [] readInstream (InputStream inputStream) throws Exception {30 // create ByteArrayOutputStream class 31 ByteA RrayOutputStream baoStream = new ByteArrayOutputStream (); 32 byte [] buffer = new byte [1024]; // declare the buffer 33 int length =-1; // define the default length of 34 while (length = inputStream. read (buffer ))! =-1) {35 baoStream. write (buffer, 0, length); // output the buffer to the memory 36} 37 38 baoStream. close (); // close output stream 39 inputStream. close (); // close the input stream 40 return baoStream. toByteArray (); // returns the byte array of the output stream 41} 42}View Code

5. Write the core code MainActivity. java class. The specific code is as follows:

1 package com. example. getdatatointernet; 2 3 import android. annotation. suppressLint; 4 import android. graphics. bitmap; 5 import android. graphics. bitmapFactory; 6 import android. OS. bundle; 7 import android. OS. strictMode; 8 import android. support. v4.app. fragment; 9 import android. support. v7.app. actionBarActivity; 10 import android. util. log; 11 import android. view. layoutInflater; 12 import android. view. menu; 13 import android. view. menuItem; 14 import android. view. view; 15 import android. view. viewGroup; 16 import android. widget. button; 17 import android. widget. editText; 18 import android. widget. imageView; 19 import android. widget. toast; 20 21 @ SuppressLint ("ValidFragment") 22 public class MainActivity extends ActionBarActivity {23 24 @ Override 25 protected void onCreate (Bundle savedInstanceState) {26 super. onCreate (savedInstanceState); 27 setContentView (R. layout. activity_main); 28 29 if (savedInstanceState = null) {30 getSupportFragmentManager (). beginTransaction () 31. add (R. id. container, new PlaceholderFragment ()). commit (); 32} 33} 34 35 @ Override 36 public boolean onCreateOptionsMenu (Menu menu) {37 38 // Inflate the menu; this adds items to the action bar if it is present. 39 getMenuInflater (). inflate (R. menu. main, menu); 40 return true; 41} 42 43 @ Override 44 public boolean onOptionsItemSelected (MenuItem item) {45 // Handle action bar item clicks here. the action bar will 46 // automatically handle clicks on the Home/Up button, so long 47 // as you specify a parent activity in AndroidManifest. xml. 48 int id = item. getItemId (); 49 if (id = R. id. action_settings) {50 return true; 51} 52 return super. onOptionsItemSelected (item); 53} 54 55/** 56 * A placeholder fragment containing a simple view. 57 */58 public class extends Fragment {59 60 public PlaceholderFragment () {61} 62 63 @ Override 64 public View onCreateView (LayoutInflater inflater, ViewGroup container, 65 Bundle savedInstanceState) {66 View rootView = inflater. inflate (R. layout. fragment_main, container, 67 false); 68 69 StrictMode. setThreadPolicy (new 70 StrictMode. threadPolicy. builder (). detectDiskReads (). detectDiskWrites (). detectNetwork (). penaltyLog (). build (); 71 StrictMode. setVmPolicy (72 new StrictMode. vmPolicy. builder (). detectLeakedSqlLiteObjects (). detectLeakedClosableObjects (). penaltyLog (). penaltyDeath (). build (); 73 74 final EditText imageText = (EditText) rootView. findViewById (R. id. path); 75 final ImageView imageView = (ImageView) rootView. findViewById (R. id. imageView); 76 final Button btn = (Button) rootView. findViewById (R. id. sendBtn); 77 78 btn. setOnClickListener (new View. onClickListener () {79 80 @ Override 81 public void onClick (View v) {82 // TODO Auto-generated method stub 83 String path = imageText. getText (). toString (); 84 try 85 {86 byte [] data = NetTool. getImage (path); 87 Bitmap bm = BitmapFactory. decodeByteArray (data, 0, data. length); 88 imageView. setImageBitmap (bm); 89} catch (Exception e) 90 {91 Log. I ("MyTest", e. getMessage (); 92 Toast. makeText (MainActivity. this, e. getMessage (), 1 ). show (); 93} 94} 95}); 96 return rootView; 97} 98} 99 100}View Code

6. Add service permissions to AndroidManifest. xml.

<Uses-permission android: name = "android. permission. INTERNET"/>

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.