MediaScanner locale学习

Windows-1253 http://msdn.microsoft.com/zh-cn/goglobal/cc305146.aspx

Russia Windows 1251 http://msdn.microsoft.com/zh-cn/goglobal/cc305144.aspx

Windows # Code Page 1256 (WinArabic)  http://msdn.microsoft.com/zh-cn/goglobal/cc305149.aspx

hebrew


MediaScannerService.java

private MediaScanner createMediaScanner() { MediaScanner scanner = new MediaScanner(this); Locale locale = getResources().getConfiguration().locale; if (locale != null) { String language = locale.getLanguage(); String country = locale.getCountry(); String localeString = null; if (language != null) { if (country != null) { scanner.setLocale(language + "_" + country); } else { scanner.setLocale(language); } } } return scanner; }

 

MediaScannerClient.cpp

MediaScannerClient::MediaScannerClient() : mNames(NULL), mValues(NULL), mLocaleEncoding(kEncodingNone) //初始化 { }

 

 

MediaScanner调用setLocale设置当前locale。

void MediaScannerClient::setLocale(const char* locale) { if (!locale) return; if (!strncmp(locale, "ja", 2)) mLocaleEncoding = kEncodingShiftJIS; else if (!strncmp(locale, "ko", 2)) mLocaleEncoding = kEncodingEUCKR; else if (!strncmp(locale, "zh", 2)) { if (!strcmp(locale, "zh_CN")) { // simplified chinese for mainland China mLocaleEncoding = kEncodingGBK; } else { // assume traditional for non-mainland Chinese locales (Taiwan, Hong Kong, Singapore) mLocaleEncoding = kEncodingBig5; } } //注意:除ja/ko/zh外,mLocaleEncoding 保持不变 }

 

 

查询可能的编码:   这里假定是Latin-1编码,假如是其他Unicode编码会失败??

static uint32_t possibleEncodings(const char* s) { uint32_t result = kEncodingAll; // if s contains a native encoding, then it was mistakenly encoded in utf8 as if it were latin-1 // so we need to reverse the latin-1 -> utf8 conversion to get the native chars back uint8_t ch1, ch2; uint8_t* chp = (uint8_t *)s; while ((ch1 = *chp++)) { if (ch1 & 0x80) { ch2 = *chp++; ch1 = ((ch1 << 6) & 0xC0) | (ch2 & 0x3F); // ch1 is now the first byte of the potential native char ch2 = *chp++; if (ch2 & 0x80) ch2 = ((ch2 << 6) & 0xC0) | (*chp++ & 0x3F); // ch2 is now the second byte of the potential native char int ch = (int)ch1 << 8 | (int)ch2; result &= findPossibleEncodings(ch); } // else ASCII character, which could be anything } return result; }

你可能感兴趣的:(String,null,character,byte,encoding)