Android:为文件名创建唯一的字符串
我正在为图像查看器创建一个android应用程序.该应用程序将下载图像,并将其存储在缓存文件夹中.
I'm doing an android application for image viewer. This app will download the image, and store them in a cache folder.
因此,在缓存文件夹中,图像文件名必须唯一.目前,我使用String.hashCode()生成文件名.
So, in the cache folder, the image file name must be unique. Currently, I use String.hashCode() to generate the file name.
还有其他更好的方法来获取唯一字符串吗?
Are there any other better ways to get unique string?
使用 java.util.UUID .查看 randomUUID ,它会生成所谓的
Use java.util.UUID. Look at randomUUID that generates a so-called Universally unique identifier.
我不太了解您打算如何使用String.hashCode生成唯一"值.您在什么字符串上调用hashCode? hashCode的目的不是生成唯一的ID ...而是为字符串生成哈希代码,因此,如果字符串本身也不是唯一的,哈希码也不会.
I don't really understand how you intend to generate a "unique" value with String.hashCode. On what string do you call hashCode? hashCode's purpose is not to generate unique IDs... It's meant to generate a hash code for a string, so if the strings themselves are not unique, the hash codes won't be either.