【Android网络编程】获取网络图片,具有缓存功能
2015-10-27 20:10
591 查看
<?xml version="1.0" encoding="utf-8"?> <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"> <TextView android:text="查看网络图片" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" /> <ImageView android:layout_width="match_parent" android:layout_height="300dp" android:id="@+id/imageView" android:layout_below="@+id/textView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下载图片" android:id="@+id/button" android:layout_below="@+id/imageView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>
package qindachang.internetdemo; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class MainActivity extends AppCompatActivity { private ImageView imageView; private Button button; private MyThread myThread; private Bitmap bitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myThread = new MyThread(); myThread.start(); } }); } private void GetInternetImg() { //下载图片 //1.确定网址 String path = "http://img2.imgtn.bdimg.com/it/u=1031079800,2914158222&fm=21&gp=0.jpg"; //②带缓存功能的读取 File file = new File(getCacheDir(), getFileName(path)); //如果缓存存在 if (file.exists()) { //从缓存中读取图片 bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); mHandler.sendEmptyMessage(0); } else { //2.把网址封装成url对象 try { URL url = new URL(path); //3.获取客户端和服务器的连接对象,此时还没有建立连接,这时候还没有任何的网络交互 HttpURLConnection connection= (HttpURLConnection) url.openConnection(); //4.对连接对象进行初始化 //设置请求方法 connection.setRequestMethod("GET"); //设置连接超时时间 connection.setConnectTimeout(5000); //设置读取超时 connection.setReadTimeout(3000); //5.发送请求,与服务器建立连接,这段代码一旦执行,就会有网络交互了 connection.connect(); //如果响应码为200,说明请求成功 if (connection.getResponseCode() == 200) { //获取服务器响应头中的流,流里的数据就是客户端请求的数据 InputStream is = connection.getInputStream(); //从流里读数据,安卓有一个API可以从流里直接读取图片 FileOutputStream fos = new FileOutputStream(file); byte[] bytes = new byte[1024]; int len = 0; while ((len = is.read(bytes)) != -1) { fos.write(bytes, 0, len); } fos.close(); bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); //①从里读取数据,并构造成位图对象 // bitmap = BitmapFactory.decodeStream(is); mHandler.sendEmptyMessage(0); } else { Toast.makeText(MainActivity.this,"请求失败",Toast.LENGTH_SHORT).show(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } private Handler mHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { imageView.setImageBitmap(bitmap); return false; } }); private class MyThread extends Thread { @Override public void run() { super.run(); GetInternetImg(); } } private String getFileName(String path) { String[] name = path.split(","); return name[1]; } }
[/code]
相关文章推荐
- 【转】HTTP Response Header 的 Content-Disposition
- 变更管理、信息系统安全管理及项目风险管理作业
- HTTP/1.1协议(中文归纳版)
- ubuntu限制本地网速
- IOS网络笔记--地图内容3(点击获取当前地址的具体坐标)
- 使用TCP从客户端上传图片到服务器端
- netstat查看网络信息
- httpclient4.3设置代理请求
- IOS网络笔记--地图内容2(正反向编码)
- 【工具类】安卓开发 HttpPost和HttpGet请求
- HttpClient使用详解
- 通过URLHttpConnection方式来取得图片,并且显示在ImageView上
- Java IO: 网络
- 同行不同命:中美两国的“网络水军”有哪些差异
- openfire因为网络不稳定而造成消息丢失的解决方案
- TCP/IP
- IdHttpServer实现webservice
- HTTPS 101
- HTTP相关概念
- IOS网络笔记--地图内容1