Http异步下载图片

作者:追风剑情 发布于:2015-8-4 21:50 分类:Android

在AndroidManifest.xml中添加权限配置

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

视图

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. tools:context="${relativePackage}.${activityClass}" >
  7.  
  8. <ImageView
  9. android:id="@+id/img"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_gravity="center" />
  13.  
  14. </LinearLayout>

活动

  1. package com.example.androidtest;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8.  
  9. import android.app.Activity;
  10. import android.graphics.Bitmap;
  11. import android.graphics.BitmapFactory;
  12. import android.os.AsyncTask;
  13. import android.os.Bundle;
  14. import android.util.Log;
  15. import android.view.Menu;
  16. import android.view.MenuItem;
  17. import android.widget.ImageView;
  18.  
  19. public class NetworkingActivity extends Activity {
  20.  
  21. ImageView img;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_networking);
  26. String url = "http://s01.incnjp.com/forum/201506/23/151249wl69l7kl725qwzpe.jpg";
  27. new DownloadImageTask().execute(url);
  28. }
  29. private InputStream OpenHttpConnection(String urlString) throws IOException
  30. {
  31. InputStream in = null;
  32. int response = -1;
  33. URL url = new URL(urlString);
  34. URLConnection conn = url.openConnection();
  35. if(!(conn instanceof HttpURLConnection))
  36. throw new IOException("Not an HTTP connection");
  37. try {
  38. HttpURLConnection httpConn = (HttpURLConnection) conn;
  39. httpConn.setAllowUserInteraction(false);
  40. httpConn.setInstanceFollowRedirects(true);
  41. httpConn.setRequestMethod("GET");
  42. httpConn.connect();
  43. response = httpConn.getResponseCode();
  44. if(response == HttpURLConnection.HTTP_OK){
  45. in = httpConn.getInputStream();
  46. }
  47. } catch (Exception ex) {
  48. Log.d("AndroidTest", ex.getLocalizedMessage());
  49. throw new IOException("Error connecting");
  50. }
  51. return in;
  52. }
  53. private Bitmap DownloadImage(String url){
  54. Bitmap bitmap = null;
  55. InputStream in = null;
  56. try {
  57. in = OpenHttpConnection(url);
  58. bitmap = BitmapFactory.decodeStream(in);
  59. in.close();
  60. } catch (IOException ex){
  61. Log.d("AndroidTest", ex.getLocalizedMessage());
  62. }
  63. return bitmap;
  64. }
  65. private class DownloadImageTask extends AsyncTask<String, Void, Bitmap>
  66. {
  67. protected Bitmap doInBackground(String... urls){
  68. return DownloadImage(urls[0]);
  69. }
  70. protected void onPostExecute(Bitmap result){
  71. ImageView img = (ImageView) findViewById(R.id.img);
  72. img.setImageBitmap(result);
  73. }
  74. }
  75. }

运行效果

111111111.png


标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号