AsyncTask下载网络图片的简单应用



1、imageTest
  1. package lpc.com.asynctaskdemo;
  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.ImageView;
  9. import android.widget.ProgressBar;
  10. import java.io.BufferedInputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.net.URL;
  14. import java.net.URLConnection;
  15. /**
  16. * Created by Administrator on 2015/12/8.
  17. */
  18. public class ImageTest extends Activity {
  19. private ImageView mImageView;
  20. private ProgressBar mProgressBar;
  21. private String URL = "http://cdnq.duitang.com/uploads/item/201409/14/20140914172442_sjXYv.jpeg";
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.image);
  26. mImageView = (ImageView) findViewById(R.id.image);
  27. mProgressBar = (ProgressBar) findViewById(R.id.progress);
  28. new MyAsycTask().execute(URL);
  29. }
  30. class MyAsycTask extends AsyncTask<String,Void,Bitmap>{
  31. @Override
  32. protected void onPreExecute() {
  33. super.onPreExecute();
  34. mProgressBar.setVisibility(View.VISIBLE);
  35. }
  36. @Override
  37. protected void onPostExecute(Bitmap bitmap) {
  38. super.onPostExecute(bitmap);
  39. mProgressBar.setVisibility(View.GONE);
  40. mImageView.setImageBitmap(bitmap);
  41. }
  42. @Override
  43. protected Bitmap doInBackground(String... params) {
  44. String url = params[0];
  45. Bitmap bitmap = null;
  46. URLConnection connection;
  47. InputStream is;
  48. try {
  49. connection = new URL(url).openConnection();
  50. is = connection.getInputStream();
  51. BufferedInputStream bis = new BufferedInputStream(is);
  52. bitmap = BitmapFactory.decodeStream(bis);
  53. is.close();
  54. bis.close();
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. }
  58. return bitmap;
  59. }
  60. }
  61. }
2、布局文件 image.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ImageView
  6. android:id="@+id/image"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent" />
  9. <ProgressBar
  10. android:id="@+id/progress"
  11. android:visibility="gone"
  12. android:layout_centerInParent="true"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content" />
  15. </RelativeLayout>
3、Manifest文件

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="lpc.com.asynctaskdemo">
  3. <uses-permission android:name="android.permission.INTERNET"/>
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:supportsRtl="true"
  9. android:theme="@style/AppTheme">
  10. <activity android:name=".ImageTest">
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN"/>
  13. <category android:name="android.intent.category.LAUNCHER"/>
  14. </intent-filter>
  15. </activity>
  16. </application>
  17. </manifest>





附件列表

     

    posted @ 2015-12-08 13:18  liupengcheng  阅读(197)  评论(0编辑  收藏  举报