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

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

activity_image.xml代码

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.jt.http_01.LoadImage"><ImageViewandroid:id="@+id/imageView1"android:layout_width="wrap_content"android:layout_height="wrap_content"/></RelativeLayout>

HttpLoadImage.java代码

/***加载网络图片(先下载到本地SD卡,再读取本地文件显示图片)*@authorjiatao*@date2015-5-2*@version1.0*/packagecom.jt.http_01;importandroid.app.Activity;importandroid.os.Bundle;importandroid.os.Handler;importandroid.widget.ImageView;publicclassHttpLoadImageextendsActivity{privateImageViewimageView;privateHandlerhandler=newHandler();@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_image);initDisplay();}publicvoidinitDisplay(){//TODOAuto-generatedmethodstubimageView=(ImageView)findViewById(R.id.imageView1);newHttpImageThread("http://img31.mtime.cn/pi/2015/02/03/111506.56197775_1000X1000.jpg",imageView,handler).start();}}

HttpImageThread.java代码

/***加载网络图片(先下载到本地SD卡,再读取本地文件显示图片)*@authorjiatao*@date2015-5-2*@version1.0*/packagecom.jt.http_01;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.net.HttpURLConnection;importjava.net.MalformedURLException;importjava.net.URL;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.os.Environment;importandroid.os.Handler;importandroid.widget.ImageView;publicclassHttpImageThreadextendsThread{privateStringurl;privateImageViewimageView;privateHandlerhandler;publicHttpImageThread(Stringurl,ImageViewimageView,Handlerhandler){this.url=url;this.imageView=imageView;this.handler=handler;}@Overridepublicvoidrun(){//TODOAuto-generatedmethodstubsuper.run();try{URLhttpUrl=newURL(url);try{HttpURLConnectionconn=(HttpURLConnection)httpUrl.openConnection();conn.setReadTimeout(5000);conn.setRequestMethod("GET");conn.setDoInput(true);//获取字节输入流InputStreamin=conn.getInputStream();FileOutputStreamout=null;Filedownloadfile=null;StringfileName=String.valueOf(System.currentTimeMillis());//判断SD卡是否存在if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){Fileparent=Environment.getExternalStorageDirectory();downloadfile=newFile(parent,fileName);//在SD卡中创建图片输出目录和图片名称out=newFileOutputStream(downloadfile);}byte[]b=newbyte[2*1024];intlen;if(out!=null){//如果输出不为空,就循环读取下载到本地while((len=in.read(b))!=-1){out.write(b,0,len);}}//BitmapFactory.decodeFile(StringpathName)把本地文件转化成Bitmap文件finalBitmapbitmap=BitmapFactory.decodeFile(downloadfile.getAbsolutePath());//向主线程发送消息,加载Bitmap文件handler.post(newRunnable(){@Overridepublicvoidrun(){//TODOAuto-generatedmethodstubimageView.setImageBitmap(bitmap);}});}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}catch(MalformedURLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}

更多相关文章

  1. Android(安卓)Studio 配置多个代码仓库(maven)
  2. Android:用ViewPager实现广告栏
  3. android设置wallpaper
  4. Android(安卓)CollapsingToolbarLayout:将ActionBar载入Toolbar(2)
  5. Android(安卓)为Notification加上一个进度条
  6. 第一篇 GridView控件
  7. 利用xml文件创建Menu
  8. android dialog和activity 改变出现消失的 动画样式
  9. Android(安卓)DrawerLayout 使用

随机推荐

  1. Android常用的几种资源
  2. Android之打造ListView的万能适配器
  3. android service 详解
  4. [置顶] Android基本框架学习之defaultSer
  5. Android键盘“enter”键设置为“下一项”
  6. Android之父Andy Rubin:被乔布斯羡慕嫉妒
  7. Android的DrawerLayout全屏滑动显示
  8. [Android] emualtor-5554 offline的解决
  9. Android(安卓)Widget开发详解
  10. Framework篇 - Android 系统介绍和架构一