admin管理员组文章数量:1639574
Android系统中U盘卷名识别异常
类别
问题缺陷
索引类别
U盘卷名乱码 无法正确识别中文
问题描述
U盘在windows下或者在Linux环境下,将对应的U盘卷名修改为名字或者命名有多国语言的时候,通常在Android系统下会不能正常识别到正确的卷名。那么下面修改就是可以正确的支持:
代码关联
对应代码如下:(android/external/e2fsprogs/misc/blkid.c )
diff --git a/misc/blkid.c b/misc/blkid.c
index 5aa949e..0dc3512 100644
--- a/misc/blkid.c
+++ b/misc/blkid.c
@@ -55,11 +55,15 @@ int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen
char **pout = &outbuf;
cd = iconv_open(to_charset, from_charset);
- if (cd == 0)
+ if (cd == 0) {
+ iconv_close(cd);
return -1;
+ }
memset(outbuf, 0, outlen);
- if (iconv(cd, pin, &inlen, pout, &outlen) == (size_t)(-1))
+ if (iconv(cd, pin, &inlen, pout, &outlen) == (size_t)(-1)) {
+ iconv_close(cd);
return -2;
+ }
iconv_close(cd);
return 1;
}
@@ -88,10 +92,11 @@ void print_hex(char *s)
int ansi_to_utf8(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
ALOGD("ansi_to_utf8...");
+ int count = 0;
char szSysLocale[PROP_VALUE_MAX] = { 0 };
char szFromLang[50]="GB2312";
- property_get("persist.sys.locale", szSysLocale, "");
+ //默认设置地区为中国
+ property_get("persist.sys.locale", szSysLocale, "zh-CN");
//Preferred to use locale transcode
/*print_hex(inbuf); for debug only, don't unmark it at release */
if(strcmp(szSysLocale, "zh-CN")==0) // china
strcpy(szFromLang, "GB2312");
@@ -103,8 +108,31 @@ int ansi_to_utf8(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
strcpy(szFromLang, "KOREAN");
else strcpy(szFromLang, "US");
- ALOGD("Get locale: %s, convert to: %s", szSysLocale, szFromLang);
- int iRet = code_convert(szFromLang, "utf-8", inbuf, inlen, outbuf, outlen);
+ //添加loop的原因是:是因为对应的如果只会判断一次用什么编码来解析文字,如果是不同国家的文字那么就要将对应的编码修改
+ //对应的不同国家的编码不同,输出的值(outbuf)也不同,如果为空可进行下一个编码方式进行尝试解码
+loop:
+ iRet = code_convert(szFromLang, "utf-8", inbuf, inlen, outbuf, outlen);
+ ALOGD("Get locale: %s, convert to: %s ,%d", szSysLocale, szFromLang,iRet);
+ if (strlen(outbuf) == 0 && count <= 4) {
+ //second-choice transcode
+ switch (count) {
+ case 0:
+ strcpy(szFromLang, "GB2312");break;
+ case 1:
+ strcpy(szFromLang, "BIG5-HKSCS:2004");break;
+ case 2:
+ strcpy(szFromLang, "SHIFT_JIS");break;
+ case 3:
+ strcpy(szFromLang, "KOREAN");break;
+ case 4:
+ strcpy(szFromLang, "US");break;
+ default :
+ break;
+ }
+ count++;
+ goto loop;
+ } else {
+ ALOGD(" There are still loopholes in (utf-8) transcoding ");
+ }
+
if(iRet!=1)
strcpy(outbuf, inbuf);
return iRet;
@@ -145,7 +173,7 @@ static void safe_print(const char *cp, int len, int iPartitionType, int iConv)
{
char out[200] = "";
int iReturnCode = 0;
-
+ //RT uniform transcoding utf-8
if(iConv){
switch(iPartitionType)
{
@@ -157,7 +185,9 @@ static void safe_print(const char *cp, int len, int iPartitionType, int iConv)
iReturnCode = ansi_to_utf8((char*)cp, strlen(cp), out, 200); break;
}
ALOGD("safe_print - iconv ReturnCode=%d", iReturnCode);
- fputs(out, stdout);
+ if (fputs(out, stdout) == EOF) {
+ ALOGD("write error");
+ }
}
else{
unsigned char ch;
改进建议
如果编码方式过多的情况下,相应的解码可能会有错误的解码方式,最好是能做到一种全球统一的编码方式,这样就不存在解码为乱码的情况了。
工作记录…
版权声明:本文标题:Android系统中U盘卷名识别异常 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1729287581a1194162.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论