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下载网络图片的简单应用的更多相关文章

  1. AsyncTask下载网络图片

    MyTask task = new MyTask(); task.execute(url); class MyTask extends AsyncTask<String, Integer, Bi ...

  2. Android初学-AsyncTask下载网络图片

    AsyncTask 异步处理: mainfest: 注意添加的: -------------------- <uses-permission android:name="android ...

  3. Android 用AsyncTask下载网络图片并显示百分比

    1.添加布局文件:activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a ...

  4. 使用url下载网络图片以及流介绍

    使用url下载网络图片的时候,首先需要建立一个URL对象,然后使用一个输入流获取该URL中的内容.之后使用读取该输入流的内容,使用一个输出流写到本地文件中.最后关闭输入和输出流.下面是一个简单的下载代 ...

  5. .Net 使用爬虫下载网络图片到本地磁盘

    准备: 1.新建控制台项目 2.引用System.Drawing类库 3.安装HtmlAgilityPack 1.5.2.0 4.如果不会XPath语法的话,建议简单看下 代码: static voi ...

  6. Android 异步任务——AsyncTask (附使用AsyncTask下载图片Demo)

    我们编程的时候经常需要处理同步任务和异步任务,在Android里面存在一个特性,就是UI线程是不安全的线程.所谓UI线程不安全也就是我们的主线程(进程启动的第一个线程)不能在线程外操作主线程的资源.因 ...

  7. Go语言下载网络图片或文件

    最近闲来无事, 于是就简单学习了下Go语言的基本的用法.由于实践才是最快的学习方法,所以这里就以下载网络图片或文件入手来学习Go语言 文件下载到本地,通常的思路就是先获得网络文件的 输入流 以及本地文 ...

  8. Android开发-下载网络图片并显示到本地

    Android下载网络图片的流程是: 发送网络请求->将图片以流的形式下载下来->将流转换为Bitmap并赋给ImageView控件. 注意点 最新的Android系统不可以在主线程上请求 ...

  9. Python下载网络图片方法汇总与实现

    本文介绍下载python下载网络图片的方法,包括通过图片url直接下载.通过re/beautifulSoup解析html下载以及对动态网页的处理等. ​ 很多人学习python,不知道从何学起.很多人 ...

随机推荐

  1. Razor基础语法简介

    http://blog.csdn.net/pasic/article/details/7072340 Razor的出现,使页面看起更加简洁,Razor的页面后缀为:.cshtml Razor基础语法: ...

  2. MVC 全局异常处理及禁用显示头

    MVC网站的global.asax中的Application_Start方法里,有这样一段代码: public class MvcApplication : System.Web.HttpApplic ...

  3. Type Project has no default.properties file! Edit the project properties to set one.

    Description Resource Path Location Type Project has no default.properties file! Edit the project pro ...

  4. GridView使用自带分页功能时分页方式及样式PagerStyle

    // 转向地址:http://www.bubuko.com/infodetail-412562.html GridView分页,使用自带分页功能,类似下面样式: 在aspx页面中,GridView上的 ...

  5. 在VBA中调用winsock控件

    如果系统没有Winsock控件的话,可以下载下面的控件MSWINSCK.OCX,然后将该文件复制到C:\Windows\System32目录下. 在VBE窗口中,从菜单"工具"-& ...

  6. ADF_Starting系列9_使用EJB/JPA/JSF通过ADF构建Web应用程序之测试J2EE Container

    2013-05-01 Created By BaoXinjian

  7. FileUploadInterceptor拦截器的笔记

    当请求表单中包含一个文件file,FileUploadInterception拦截器会自动应用于这个文件. 表单: <s:form namespace="/xxx" acti ...

  8. [Linux] Linux下谁在消耗我们的cache

    一.缘由: 曾经看到MySQL服务器上Cache占用特别大,其实大家都知道这是合理的,这些都是可用内存: 那么问题来了,是谁在占用这些Cache呢?如果去掉不合理的Cache占用,系统内存会更充分的得 ...

  9. Java 开发必会的 Linux 命令

    作为一个Java开发人员,有些常用的Linux命令必须掌握.即时平时开发过程中不使用Linux(Unix)或者mac系统,也需要熟练掌握Linux命令.因为很多服务器上都是Linux系统.所以,要和服 ...

  10. Fighting Game

    感谢上外静中任淳同学提供   uses crt; label   h;         //h是重新开始游戏 const   y1=18;   y2=18;       //p1,p2的纵坐标 var ...