Skip to content

Instantly share code, notes, and snippets.

@mcxiaoke
Created April 29, 2014 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcxiaoke/11389156 to your computer and use it in GitHub Desktop.
Save mcxiaoke/11389156 to your computer and use it in GitHub Desktop.
SafeFileNameGenerator for UniversalImageLoader
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