码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-12-08 14:26:28      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

技术分享技术分享技术分享

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>





附件列表

     

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

    标签:

    原文地址:http://www.cnblogs.com/liupengcheng/p/5028795.html

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!