2024年安卓最全Android中图片的三级缓存介绍及实现(1),2024年最新大厂面试题java

《960全网最全Android开发笔记》

《379页Android开发面试宝典》

《507页Android开发相关源码解析》

因为文件太多,全部展示会影响篇幅,暂时就先列举这些部分截图

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

17 public class HttpUtils {

18 /**

19 * 判断网络连接是否通畅

20 * @param mContext

21 * @return

22 */

23 public static boolean isNetConn(Context mContext) {

24 ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);

25 NetworkInfo info = manager.getActiveNetworkInfo();

26 if (info != null) {

27 return info.isConnected();

28 } else {

29 return false;

30 }

31 }

32

33 /**

34 * 根据path下载网络上的数据

35 * @param path 路径

36 * @return 返回下载内容的byte数据形式

37 */

38 public static byte[] getDateFromNet(String path) {

39 ByteArrayOutputStream baos = new ByteArrayOutputStream();

40 try {

41 URL url = new URL(path);

42 HttpURLConnection conn = (HttpURLConnection) url.openConnection();

43 conn.setRequestMethod(“GET”);

44 conn.setConnectTimeout(5000);

45 conn.setDoInput(true);

46 conn.connect();

47 if (conn.getResponseCode()==200) {

48 InputStream is = conn.getInputStream();

49 byte b[] = new byte[1024];

50 int len;

51 while ((len=is.read(b))!=-1) {

52 baos.write(b, 0, len);

53 }

54 return baos.toByteArray();

55 }

56 } catch (IOException e) {

57 e.printStackTrace();

58 }

59 return baos.toByteArray();

60 }

61

62 }

复制代码

还有操作外部存储的工具类:

复制代码

1 package com.yztc.lx.cashimg; 2

3 import android.graphics.Bitmap; 4 import android.graphics.BitmapFactory; 5 import android.os.Environment; 6

7 import java.io.ByteArrayOutputStream; 8 import java.io.File; 9 import java.io.FileInputStream;

10 import java.io.FileOutputStream;

11 import java.io.IOException;

12

13 /**

14 * Created by Lx on 2016/8/20.

15 */

16

17 public class ExternalStorageUtils {

18 /**

19 * 将传递过来的图片byte数组存储到sd卡中

20 * @param imgName 图片的名字

21 * @param buff byte数组

22 * @return 返回是否存储成功

23 */

24 public static boolean storeToSDRoot(String imgName, byte buff[]) {

25 boolean b = false;

26 String basePath = Environment.getExternalStorageDirectory().getAbsolutePath();

27 File file = new File(basePath, imgName);

28 try {

29 FileOutputStream fos = new FileOutputStream(file);

30 fos.write(buff);

31 fos.close();

32 b = true;

33 } catch (IOException e) {

34 e.printStackTrace();

35 }

36 return b;

37 }

38

39 /**

40 * 从本地内存中根据图片名字获取图片

41 * @param imgName 图片名字

42 * @return 返回图片的Bitmap格式

43 */

44 public static Bitmap getImgFromSDRoot(String imgName) {

45 Bitmap bitmap = null;

46 String basePath = Environment.getExternalStorageDirectory().getAbsolutePath();

47 File file = new File(basePath, imgName);

48 try {

49 FileInputStream fis = new FileInputStream(file);

50 ByteArrayOutputStream baos = new ByteArrayOutputStream();

51 byte b[] = new byte[1024];

52 int len;

53 while ((len = fis.read(b)) != -1) {

54 baos.write(b, 0, len);

55 }

56 byte buff[] = baos.toByteArray();

57 if (buff != null && buff.length != 0) {

58 bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);

59 }

60 } catch (IOException e) {

61 e.printStackTrace();

62 }

63 return bitmap;

64 }

65

66

67 }

复制代码

本例中将图片默认存在了sd卡根目录中。

然后是最主要的主函数了:

复制代码

1 package com.yztc.lx.cashimg; 2

3 import android.graphics.Bitmap; 4 import android.graphics.BitmapFactory; 5 import android.os.Bundle; 6 import android.os.Handler; 7 import android.os.Message; 8 import android.support.v7.app.AppCompatActivity; 9 import android.util.Log; 10 import android.util.LruCache; 11 import android.view.View; 12 import android.widget.Button; 13 import android.widget.ImageView; 14 import android.widget.Toast; 15

16 import java.lang.ref.SoftReference; 17 import java.util.LinkedHashMap; 18

19 public class MainActivity extends AppCompatActivity implements View.OnClickListener { 20

21 private Button btn_download; 22 private ImageView iv_img; 23 private MyLruCache myLruCache; 24 private LinkedHashMap<String, SoftReference> cashMap = new LinkedHashMap<>();

25 private static final String TAG = “MainActivity”;

26 private String imgPath = “http://www.3dmgame.com/UploadFiles/201212/Medium_20121217143424221.jpg”;

27 private Handler handler = new Handler() { 28 @Override

29 public void handleMessage(Message msg) { 30 Bitmap bitmap = (Bitmap) msg.obj; 31 iv_img.setImageBitmap(bitmap);

32 Toast.makeText(MainActivity.this, “从网络上下载图片”, Toast.LENGTH_SHORT).show();

33 }

34 };

35

36 @Override

37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState);

39 setContentView(R.layout.activity_main);

40 initView();

41 int totalMemory = (int) Runtime.getRuntime().maxMemory();

42 int size = totalMemory / 8;

43 myLruCache = new MyLruCache(size); 44 btn_download.setOnClickListener(this);

45 }

46

47 private void initView() { 48 btn_download = (Button) findViewById(R.id.btn_download); 49 iv_img = (ImageView) findViewById(R.id.iv_img); 50 }

51

52 @Override

53 public void onClick(View v) { 54 Bitmap b = getImgCache(); 55 if (b != null) {

56 iv_img.setImageBitmap(b);

57 } else { 58 new Thread(new Runnable() { 59 @Override

60 public void run() { 61 if (HttpUtils.isNetConn(MainActivity.this)) {

62 byte b[] = HttpUtils.getDateFromNet(imgPath); 63 if (b != null && b.length != 0) {

64 Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);

65 Message msg = Message.obtain(); 66 msg.obj = bitmap; 67 handler.sendMessage(msg);

68 myLruCache.put(imgPath, bitmap);

69 Log.d(TAG, "run: " + “缓存到强引用中成功”);

70 boolean bl = ExternalStorageUtils.storeToSDRoot(“haha.jpg”, b);

71 if (bl) { 72 Log.d(TAG, "run: " + “缓存到本地内存成功”);

73 } else { 74 Log.d(TAG, "run: " + “缓存到本地内存失败”);

75 }

76 } else { 77 Toast.makeText(MainActivity.this, “下载失败!”, Toast.LENGTH_SHORT).show();

78 }

79

80 } else { 81 Toast.makeText(MainActivity.this, “请检查你的网络!”, Toast.LENGTH_SHORT).show();

82 }

83 }

84 }).start();

85 }

86 }

87

88 /**

89 * 从缓存中获取图片

90 *

91 * @return 返回获取到的Bitmap 92 */

93 public Bitmap getImgCache() { 94 Bitmap bitmap = myLruCache.get(imgPath); 95 if (bitmap != null) {

96 Log.d(TAG, "getImgCache: " + “从LruCache获取图片”);

97 } else { 98 SoftReference sr = cashMap.get(imgPath); 99 if (sr != null) {

100 bitmap = sr.get();

101 myLruCache.put(imgPath, bitmap);

102 cashMap.remove(imgPath);

103 Log.d(TAG, "getImgCache: " + “从软引用获取图片”);

104 } else {

105 bitmap = ExternalStorageUtils.getImgFromSDRoot(“haha.jpg”);

106 Log.d(TAG, "getImgCache: " + “从外部存储获取图片”);

107 }

108 }

109 return bitmap;

110 }

111

112 /**

113 * 自定义一个方法继承系统的LruCache方法

114 */

115 public class MyLruCache extends LruCache<String, Bitmap> {

116

117 /**

118 * 必须重写的构造函数,定义强引用缓存区的大小

119 * @param maxSize for caches that do not override {@link #sizeOf}, this is

120 * the maximum number of entries in the cache. For all other caches,

121 * this is the maximum sum of the sizes of the entries in this cache.

122 */

学习分享

①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

习分享

①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包

[外链图片转存中…(img-8UhKdjJU-1715736856773)]

[外链图片转存中…(img-2kerXrL8-1715736856774)]

[外链图片转存中…(img-L13JFuGb-1715736856774)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值