【HttpUrlConnection】图片缓存+缓存加密(3)

类似【网页图片查看器】,增加了缓存机制
如果是初次加载图片,会缓存到data/data/包/cache/目录下;如果是加载以前的图片,会直接从缓存文件夹中加载图片。

注:【1】需要访问网络操作需要:
【1.1】加载权限
【1.2】使用子线程
【2】不能在子线程更新UI 需要使用handler

思路:
【1】新建一个缓存文件夹并使用Base64加密
【1.1】
File file=new File(getCacheDir(),Base64.encodeToString(path.getBytes(), Base64.DEFAULT));
【2】初次加载
【2.1】实现文件的缓存,即将文件写入到cache文件夹中。这里涉及到文件的写入操作
【3】加载缓存
【3.1】直接加载绝对路径的缓存文件夹中的图片,这里用到的是decodeFile方法
Bitmap cacheBitmap = BitmapFactory. decodeFile(file.getAbsolutePath());
【4】使用handler更新UI



代码如下:
   
   
  1. package com.example.imagedemo;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.InputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.os.Message;
  11. import android.app.Activity;
  12. import android.graphics.Bitmap;
  13. import android.graphics.BitmapFactory;
  14. import android.util.Base64;
  15. import android.view.Menu;
  16. import android.view.View;
  17. import android.widget.EditText;
  18. import android.widget.ImageView;
  19. public class MainActivity extends Activity {
  20. private EditText et_web;
  21. private ImageView iv;
  22. private Handler handler=new Handler(){
  23. public void handleMessage(Message msg) {
  24. Bitmap bitmap=(Bitmap) msg.obj;
  25. iv.setImageBitmap(bitmap);
  26. };
  27. };
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_main);
  32. //[1]找id
  33. et_web = (EditText) findViewById(R.id.et_web);
  34. iv = (ImageView) findViewById(R.id.iv);
  35. }
  36. public void click(View v){
  37. new Thread(){
  38. public void run(){
  39. try {
  40. String path=et_web.getText().toString().trim();
  41. //缓存图片
  42. //新建图片缓存地址并使用Base64加密
  43. File file=new File(getCacheDir(),Base64.encodeToString(path.getBytes(), Base64.DEFAULT));
  44. if(file.exists()&&file.length()>0){
  45. System.out.println("使用缓存");
  46. //直接从文件的绝对路径找到图片
  47. Bitmap cacheBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
  48. Message msg = Message.obtain();
  49. msg.obj=cacheBitmap;
  50. handler.sendMessage(msg);
  51. }else{
  52. System.out.println("第一次加载");
  53. //找路径
  54. //创建URL对象
  55. URL url =new URL(path);
  56. //拿到httpurlconnection对象 用于发送或接收请求
  57. HttpURLConnection conn=(HttpURLConnection) url.openConnection();
  58. //设置发送请求
  59. conn.setRequestMethod("GET");
  60. //设置超时时长
  61. conn.setConnectTimeout(5000);
  62. //接收返回码
  63. int code = conn.getResponseCode();
  64. if(code==200){
  65. InputStream in=conn.getInputStream();
  66. //初始化输出流并输出
  67. FileOutputStream fos=new FileOutputStream(file);
  68. byte[] buf=new byte[1024];
  69. int len=-1;
  70. while((len=in.read(buf))!=-1){
  71. fos.write(buf,0,len);
  72. }
  73. fos.close();
  74. in.close();
  75. //将流转换成bitmap格式
  76. Bitmap bitmap=BitmapFactory.decodeFile(file.getAbsolutePath());
  77. //设置msg对象
  78. Message msg=Message.obtain();
  79. msg.obj=bitmap;
  80. handler.sendMessage(msg);
  81. }
  82. }
  83. } catch (Exception e) {
  84. // TODO Auto-generated catch block
  85. e.printStackTrace();
  86. }
  87. }
  88. }.start();
  89. }
  90. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值