Created
April 29, 2014 02:09
-
-
Save mcxiaoke/11389156 to your computer and use it in GitHub Desktop.
SafeFileNameGenerator for UniversalImageLoader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator; | |
/** | |
* User: mcxiaoke | |
* Date: 14-4-28 | |
* Time: 11:29 | |
*/ | |
public class SafeFileNameGenerator implements FileNameGenerator { | |
@Override | |
public String generate(final String imageUri) { | |
return toSafeFileName(imageUri); | |
} | |
public static String toSafeFileName(String name) { | |
int size = name.length(); | |
StringBuilder builder = new StringBuilder(size * 2); | |
for (int i = 0; i < size; i++) { | |
char c = name.charAt(i); | |
boolean valid = c >= 'a' && c <= 'z'; | |
valid = valid || (c >= 'A' && c <= 'Z'); | |
valid = valid || (c >= '0' && c <= '9'); | |
valid = valid || (c == '_') || (c == '-') || (c == '.'); | |
if (valid) { | |
builder.append(c); | |
} else { | |
// Encode the character using hex notation | |
builder.append('x'); | |
builder.append(Integer.toHexString(i)); | |
} | |
} | |
return builder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment