1.添加布局文件:activity_main.xml
1 <LinearLayout 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:orientation="vertical" > 6 7 <ImageView 8 android:id="@+id/mImageView" 9 android:layout_width="300dp" 10 android:layout_height="300dp" /> 11 12 <ProgressBar 13 android:id="@+id/mProgressBar" 14 style="?android:attr/progressBarStyleHorizontal" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:maxHeight="10dip" 18 android:minHeight="10dip" /> 19 20 <Button 21 android:id="@+id/btnGetImg" 22 android:layout_width="120dp" 23 android:layout_height="50dp" 24 android:text="Getmage" /> 25 26 <Button 27 android:id="@+id/btnAbort" 28 android:layout_width="120dp" 29 android:layout_height="50dp" 30 android:text="Abort" /> 31 32 </LinearLayout>
2.添加java文件MainActivity.java
1 package com.example.loadimgfrominternet; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.net.HttpURLConnection; 7 import java.net.MalformedURLException; 8 import java.net.URL; 9 import java.net.URLConnection; 10 11 import android.app.Activity; 12 import android.content.Context; 13 import android.graphics.Bitmap; 14 import android.graphics.BitmapFactory; 15 import android.os.AsyncTask; 16 import android.os.Bundle; 17 import android.os.SystemClock; 18 import android.view.View; 19 import android.view.View.OnClickListener; 20 import android.widget.Button; 21 import android.widget.ImageView; 22 import android.widget.ProgressBar; 23 24 public class MainActivity extends Activity implements OnClickListener { 25 26 private Button btnGetImg; 27 private Button btnAbort; 28 private ProgressBar mProgressBar; 29 private ImageView mImageView; 30 private static final String ImageUrl = "https://images0.cnblogs.com/i/169207/201408/112229149526951.png"; 31 32 ImageLoader loader = null; 33 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.activity_main); 38 initView(); 39 } 40 41 private void initView() { 42 btnGetImg = (Button) findViewById(R.id.btnGetImg); 43 btnAbort = (Button) findViewById(R.id.btnAbort); 44 mProgressBar = (ProgressBar) findViewById(R.id.mProgressBar); 45 btnGetImg.setOnClickListener(this); 46 btnAbort.setOnClickListener(this); 47 48 mProgressBar.setVisibility(View.INVISIBLE); 49 mImageView = (ImageView) findViewById(R.id.mImageView); 50 } 51 52 @Override 53 public void onClick(View v) { 54 int id = v.getId(); 55 if (id == R.id.btnGetImg) { 56 loader = new ImageLoader(); 57 loader.execute(ImageUrl); 58 btnGetImg.setEnabled(false); 59 } else if (id == R.id.btnAbort) { 60 loader.cancel(true); 61 btnGetImg.setEnabled(true); 62 } 63 } 64 65 class ImageLoader extends AsyncTask<String, Integer, Bitmap> { 66 @Override 67 protected void onPreExecute() { 68 mProgressBar.setVisibility(View.VISIBLE); 69 mProgressBar.setProgress(0); 70 mImageView.setImageResource(R.drawable.noimg); 71 super.onPreExecute(); 72 } 73 74 @Override 75 protected Bitmap doInBackground(String... params) { 76 String imgUri = params[0]; 77 78 try { 79 URL url = null; 80 HttpURLConnection conn = null; 81 InputStream inputStream = null; 82 OutputStream outputStream = null; 83 String filename = "local_temp_image"; 84 85 try { 86 87 url = new URL(imgUri); 88 conn = (HttpURLConnection) url.openConnection(); 89 conn.setDoInput(true); 90 conn.setDoOutput(false); 91 conn.setConnectTimeout(20 * 1000); 92 inputStream = conn.getInputStream(); 93 outputStream = openFileOutput(filename, 94 Context.MODE_PRIVATE); 95 byte[] data = new byte[1024]; 96 int seg = 0; 97 long total = conn.getContentLength(); 98 long current = 0; 99 while (!isCancelled() 100 && (seg = inputStream.read(data)) != -1) { 101 outputStream.write(data, 0, seg); 102 current += seg; 103 int progress = (int) ((float) current / total * 100); 104 publishProgress(progress);// // 通知进度条UI更新 105 SystemClock.sleep(1000); 106 } 107 } finally { 108 if (conn != null) { 109 conn.disconnect(); 110 } 111 if (inputStream != null) { 112 inputStream.close(); 113 } 114 if (outputStream != null) { 115 outputStream.close(); 116 } 117 } 118 // return BitmapFactory.decodeStream(HandlerData(ImageUrl)); 119 return BitmapFactory.decodeFile(getFileStreamPath(filename) 120 .getAbsolutePath()); 121 } catch (MalformedURLException e) { 122 e.printStackTrace(); 123 } catch (IOException e) { 124 e.printStackTrace(); 125 } 126 return null; 127 } 128 129 @Override 130 protected void onProgressUpdate(Integer... values) { 131 if (isCancelled()) 132 return; 133 mProgressBar.setProgress(values[0]); 134 btnGetImg.setText(values[0] + "%"); 135 super.onProgressUpdate(values); 136 } 137 138 @Override 139 protected void onPostExecute(Bitmap bitmap) { 140 if (bitmap != null) { 141 mImageView.setImageBitmap(bitmap); 142 } 143 mProgressBar.setVisibility(View.INVISIBLE); 144 mProgressBar.setProgress(0); 145 btnAbort.setEnabled(false); 146 super.onPostExecute(bitmap); 147 } 148 } 149 150 /* 根据URI地址读取输入流 */ 151 public static InputStream HandlerData(String url) { 152 InputStream inStream = null; 153 154 try { 155 URL feedUrl = new URL(url); 156 URLConnection conn = feedUrl.openConnection(); 157 conn.setConnectTimeout(10 * 1000); 158 inStream = conn.getInputStream(); 159 } catch (Exception e) { 160 e.printStackTrace(); 161 } 162 163 return inStream; 164 } 165 }