Android加载网络图片

AndroidManifest.xml文件中加入以下权限设置:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE\_EXTERNAL\_STORAGE" />

activity_image.xml代码

1<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 6    android:paddingLeft="@dimen/activity_horizontal_margin" 7    android:paddingRight="@dimen/activity_horizontal_margin" 8    android:paddingTop="@dimen/activity_vertical_margin" 9    tools:context="com.jt.http_01.LoadImage" > 10    <ImageView 11        android:id="@+id/imageView1" 12        android:layout_width="wrap_content" 13        android:layout_height="wrap_content"/> 14</RelativeLayout>

HttpLoadImage.java代码

1/** 2 * 加载网络图片(先下载到本地SD卡,再读取本地文件显示图片)    3 * @author jiatao   4 * @date 2015-5-2   5 * @version 1.0 6 */ 7package com.jt.http_01; 8import android.app.Activity; 9import android.os.Bundle; 10import android.os.Handler; 11import android.widget.ImageView; 12public class HttpLoadImage extends Activity { 13  14 private ImageView imageView; 15 private Handler handler = new Handler(); 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18  super.onCreate(savedInstanceState); 19  setContentView(R.layout.activity_image); 20  initDisplay(); 21 } 22 public void initDisplay() { 23  // TODO Auto-generated method stub 24  imageView = (ImageView) findViewById(R.id.imageView1); 25  new HttpImageThread("http://img31.mtime.cn/pi/2015/02/03/111506.56197775_1000X1000.jpg", imageView, handler).start(); 26 }  27}

HttpImageThread.java代码

1/** 2 * 加载网络图片(先下载到本地SD卡,再读取本地文件显示图片)    3 * @author jiatao   4 * @date 2015-5-2   5 * @version 1.0 6 */ 7package com.jt.http_01; 8import java.io.File; 9import java.io.FileOutputStream; 10import java.io.IOException; 11import java.io.InputStream; 12import java.net.HttpURLConnection; 13import java.net.MalformedURLException; 14import java.net.URL; 15import android.graphics.Bitmap; 16import android.graphics.BitmapFactory; 17import android.os.Environment; 18import android.os.Handler; 19import android.widget.ImageView; 20public class HttpImageThread extends Thread { 21 private String url; 22 private ImageView imageView; 23 private Handler handler; 24  25 public HttpImageThread(String url,ImageView imageView,Handler handler){ 26  this.url = url; 27  this.imageView = imageView; 28  this.handler = handler; 29 } 30  31 @Override 32 public void run() { 33  // TODO Auto-generated method stub 34  super.run(); 35  try { 36   URL httpUrl = new URL(url); 37   try { 38    HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); 39    conn.setReadTimeout(5000); 40    conn.setRequestMethod("GET"); 41    conn.setDoInput(true); 42     43    //获取字节输入流 44    InputStream in = conn.getInputStream(); 45    FileOutputStream out = null; 46    File downloadfile = null;     47    String fileName = String.valueOf(System.currentTimeMillis()); 48    //判断SD卡是否存在 49    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ 50     File parent = Environment.getExternalStorageDirectory(); 51     downloadfile = new File(parent,fileName); 52     //在SD卡中创建图片输出目录和图片名称 53     out = new FileOutputStream(downloadfile); 54    } 55     56    byte[] b = new byte[2*1024]; 57    int len; 58    if(out != null){ 59     //如果输出不为空,就循环读取下载到本地 60     while((len = in.read(b))!=-1){ 61      out.write(b, 0, len); 62     } 63    }  64     65    //BitmapFactory.decodeFile(String pathName)把本地文件转化成Bitmap文件 66    final Bitmap bitmap = BitmapFactory.decodeFile(downloadfile.getAbsolutePath()); 67    //向主线程发送消息,加载Bitmap文件 68    handler.post(new Runnable() { 69      70     @Override      71     public void run() { 72      // TODO Auto-generated method stub 73      imageView.setImageBitmap(bitmap); 74     } 75    }); 76     77   } catch (IOException e) { 78    // TODO Auto-generated catch block 79    e.printStackTrace(); 80   }    81  } catch (MalformedURLException e) { 82   // TODO Auto-generated catch block 83   e.printStackTrace(); 84  } 85   86 } 87  88}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang

android获取mac地址

1、<usespermissionandroid:name"android.permission.ACCESS_WIFI_STATE"/ 2、privateStringgetLocalMacAddress(){WifiManagerwifi(WifiManager)getSystemSe

Android 设置系统闹铃和日历

一、创建定时器 设置权限<usespermissionandroid:name"com.android.alarm.permission.SET_ALARM"/案例https://developer.android.com/reference/android/provider/Alar

Android加载网络图片 - HelloWorld