首页 > 代码库 > Android中检测字符编码(GB2312,ASCII,UTF8,UNICODE,TOTAL——ENCODINGS)方法(二)
Android中检测字符编码(GB2312,ASCII,UTF8,UNICODE,TOTAL——ENCODINGS)方法(二)
Intent intent = getIntent();
String contentUri = null;
Uri uri =null;
if (intent.getData() != null) {
uri = intent.getData();
contentUri = "file".equals(uri.getScheme())
? FileContentProvider.BASE_URI + uri.getEncodedPath()
: uri.toString();
String intentType = intent.getType();
if (intentType != null) {
contentUri += "?" + intentType;
}
}
File mFile = new File(uri.getPath());
InputStream mIn = null;
try{
Log.d(LOGTAG,"file is "+mFile);
mIn = new FileInputStream(mFile);
byte[]b = new byte[3];
mIn.read(b);
mIn.close();
Log.d(LOGTAG,"B is "+b[0]+b[1]+b[2]);
if (b[0]==-27||(b[0]==-17&&b[1]==-69&&b[2]==-65)) {
s.setDefaultTextEncodingName("utf-8");//added by chenxuan for SWBUG00027593
}
else{
s.setDefaultTextEncodingName("GBK");
}
}catch(FileNotFoundException e){
Toast.makeText(this,R.string.file_inexistence,Toast.LENGTH_SHORT).show();
finish();
return;
}catch(IOException ex){
finish();
Toast.makeText(this, R.string.read_file_failed,Toast.LENGTH_SHORT).show();
return;
}
String contentUri = null;
Uri uri =null;
if (intent.getData() != null) {
uri = intent.getData();
contentUri = "file".equals(uri.getScheme())
? FileContentProvider.BASE_URI + uri.getEncodedPath()
: uri.toString();
String intentType = intent.getType();
if (intentType != null) {
contentUri += "?" + intentType;
}
}
File mFile = new File(uri.getPath());
InputStream mIn = null;
try{
Log.d(LOGTAG,"file is "+mFile);
mIn = new FileInputStream(mFile);
byte[]b = new byte[3];
mIn.read(b);
mIn.close();
Log.d(LOGTAG,"B is "+b[0]+b[1]+b[2]);
if (b[0]==-27||(b[0]==-17&&b[1]==-69&&b[2]==-65)) {
s.setDefaultTextEncodingName("utf-8");//added by chenxuan for SWBUG00027593
}
else{
s.setDefaultTextEncodingName("GBK");
}
}catch(FileNotFoundException e){
Toast.makeText(this,R.string.file_inexistence,Toast.LENGTH_SHORT).show();
finish();
return;
}catch(IOException ex){
finish();
Toast.makeText(this, R.string.read_file_failed,Toast.LENGTH_SHORT).show();
return;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。