android中的http通讯(2)
2015-11-23 18:31
495 查看
功能:通过网络请求下载一张图片到本地展示
在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
xml布局文件
效果图:
已经把美女图下载下来
在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
xml布局文件
<RelativeLayout xmlns: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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout><strong> </strong>MainActivity.java文件
package com.example.dd; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView contentImageView; private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); new HttpConnection("http://h.hiphotos.baidu.com/image/pic/item/6c224f4a20a446239e8d311c9b22720e0cf3d70d.jpg",contentImageView,handler).start(); } private void init(){ contentImageView = (ImageView)findViewById(R.id.imageView1); } }HttpConnection.java文件
package com.example.dd; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import android.os.Handler; import android.webkit.WebView; import android.widget.ImageView; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; /** * Created by xiaoyuer on 2015/11/13. */ public class HttpConnection extends Thread{ private String mUrl; private ImageView mImageView; private Handler mHandler; HttpConnection(String Url,ImageView imageView,Handler Handler) { this.mUrl = Url; this.mImageView = imageView; this.mHandler = Handler; } @Override public void run() { try { //构建url对象 URL httpUrl = new URL(mUrl); //通过openConnection方法打开一个HttpURLConnection对象 HttpURLConnection conn = (HttpURLConnection)httpUrl.openConnection(); //设置请求超时,访问网页时可能没给我们应答,设置超时等待时间 conn.setReadTimeout(5000); //设置访问请求方式 conn.setRequestMethod("GET"); //获得图片信息的输入流 conn.setDoInput(true); InputStream in = conn.getInputStream(); //先把图片下载到本地 FileOutputStream out = null; File downloadFile = null; //把当前的时间设置为文件名,valueOf()把long类型转换为String类型 String fileName = String.valueOf(System.currentTimeMillis()); //如果sd卡有加载 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { //获得sd卡的根目录 File parent = Environment.getExternalStorageDirectory(); //创建目录 downloadFile= new File(parent,fileName); out = new FileOutputStream(downloadFile); } //创建缓冲区(2个字节大小) byte[] b = new byte[2*1024]; int length; if(out != null){ while((length = in.read(b)) != -1){ out.write(b,0,length); } } final Bitmap bitmap = BitmapFactory.decodeFile(downloadFile.getAbsolutePath()); //读出文件之后,通过handler去更新UI mHandler.post(new Runnable() { @Override public void run() { //把已经下载来图片的bitmap传递进来 mImageView.setImageBitmap(bitmap); } }); }catch(MalformedURLException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } }
效果图:
已经把美女图下载下来
相关文章推荐
- Python 一条命令 开启 Http 服务
- iOS9 项目因使用http请求导致失败怎么处理
- 学习日记--Android第三方异步网路加载库AsyncHttpClient内部实现
- coco2d-js 无法加载网络图片(Failed to execute 'texImage2D' on ‘WebGLRenderin
- android中的http通信(1)
- 计算机网络:运输层
- 强大的网络通信框架(不实现缓存)--第三方开源--AsyncHttpClient
- Http Invoker Service & Client
- socket和TCP/IP,http的区别
- HTTP请求过程
- 利用第三方异步网路加载库AsyncHttpClient
- Asynchttpclient开源框架下载图片和文本,于Volley和Glide开源框架的区别。
- 解决ubuntu上ifconfig没有eth0且无法上网的问题
- HTTP协议版本介绍
- 开源框架 Asynchttpclient
- AsyncHttpClient 加载图片
- Android第三方异步网路加载库AsyncHttpClient
- http轮询服务器
- 网络连接与重传策略
- http://www.open-open.com/