【Android网络编程】获取网络图片,具有缓存功能
16lz
2021-01-23
<?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]; }}
更多相关文章
- Android 的缓存机制 Lrucache
- Android Bmob后端云—数据库、服务器!
- 【Android 内存优化】Bitmap 内存缓存 ( Bitmap 缓存策略 | LruC
- Android绘制文本对象FontMetrics介绍及繪製文本
- Android开发之与服务器(jsp)发送、接受JSON数据
- [置顶] Android上传文件到Web服务器,PHP接收文件(二)
- android context.getCacheDir()缓存目录
- Android异步加载图像小结 (含线程池,缓存方法)