zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    514.栅栏染色
    OOM的起点到终点
    OOM的起点到终点
    2019 Android 高级面试题总结 从java语言到AIDL使用与原理 ...
    2019 Android 高级面试题总结 从java语言到AIDL使用与原理 ...
    Vue 结合 echarts 原生 html5 实现拖拽排版报表系统
    Vue 结合 echarts 原生 html5 实现拖拽排版报表系统
    Python一行代码获得IP地址
    Python一行代码获得IP地址
    记一次Pinpoint监控工具部署过程
  • 原文地址:https://www.cnblogs.com/bill-technology/p/4130930.html
Copyright © 2011-2022 走看看