Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何为存储位图的缓存生成密钥?_Android_Image_Caching_Bitmap - Fatal编程技术网

Android 如何为存储位图的缓存生成密钥?

Android 如何为存储位图的缓存生成密钥?,android,image,caching,bitmap,Android,Image,Caching,Bitmap,我想在我的应用程序中集成此缓存机制: 现在,当我有位图时,我应该使用哪个键?或者更好:如何生成密钥 如果我可以从位图本身生成键,以便稍后调用containsString键来检查位图是否已经在缓存中,那就太好了 那么我该怎么办呢?您希望通过对位图字节进行散列来生成密钥,以确保两个位图很可能不会产生相同的密钥,除非它们是相同的 您需要将位图转换为字节数组才能使用内置库 然后,使用将密钥字节转换为字符串 Bitmap bmp = new Bitmap(); // load your bitmap...

我想在我的应用程序中集成此缓存机制:

现在,当我有位图时,我应该使用哪个键?或者更好:如何生成密钥

如果我可以从位图本身生成键,以便稍后调用containsString键来检查位图是否已经在缓存中,那就太好了


那么我该怎么办呢?

您希望通过对位图字节进行散列来生成密钥,以确保两个位图很可能不会产生相同的密钥,除非它们是相同的

您需要将位图转换为字节数组才能使用内置库

然后,使用将密钥字节转换为字符串

Bitmap bmp = new Bitmap(); // load your bitmap...
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

MessageDigest digest = MessageDigest.getInstance("SHA-256");  
digest.update(byteArray);
byte[] keyBytes = digest.digest(byteArray);