代码中获得系统分区

简介: 前言:在最近的工作中涉及到从u盘拷贝大量数据到车机,偶尔有失效的情况,后面发现是sdcard存储空间不足,因此想在代码中展示出当前系统分区.查看系统分区在adb中为adb shell df我们用如下方法可执行任意的adb shell命令,在此仅用...

前言:在最近的工作中涉及到从u盘拷贝大量数据到车机,偶尔有失效的情况,后面发现是sdcard存储空间不足,因此想在代码中展示出当前系统分区.查看系统分区在adb中为adb shell df

我们用如下方法可执行任意的adb shell命令,在此仅用"df"举例
  • 代码如下
 private String[] doRuntimeCmmd(String command) {
        Log.d(TAG, "doRuntimeCmmd:" + command);
        Process process = null;
        BufferedReader mOutReader = null;
        BufferedReader mErrorReader = null;
       try {
            process = Runtime.getRuntime().exec(command);
            Log.d(TAG, "process exec: " + process);
            
            mOutReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            int mOutread;
       char[] outBuffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((mOutread = mOutReader.read(outBuffer)) > 0) {
                output.append(outBuffer, 0, mOutread);
            }   
            mErrorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            int mErrorread;
            char[] errorBuffer = new char[4096];
            StringBuffer error = new StringBuffer();
              while ((mErrorread = mErrorReader.read(errorBuffer)) > 0) {
                error.append(errorBuffer, 0, mErrorread);
            }
            
            process.waitFor();
          String[] mResult = { output.toString(), error.toString() };
            Log.d(TAG, command +" Result:" + mResult[0]);
            Log.d(TAG, command+ " Error:" + mResult[1]);
            return mResult;
              } catch (Exception e) {
            e.printStackTrace();
            String[] mResult = { "error", "error" };
            Log.d(TAG, command + "  Result = " + mResult[0] + "   Error = " + mResult[1]);
            return mResult;
     } finally {
            try {
                if (mOutReader != null) {
                    mOutReader.close();
                }
          if (mErrorReader != null) {
                    mErrorReader.close();
                }
            if (process != null) {
                    Log.d(TAG, "process destroy: " + process);
                    process.destroy();
                }
         } catch (IOException e) {
                e.printStackTrace();
            }
                }
    }

我们在代码中按如下使用

String[] runtimeCmmd = doRuntimeCmmd("df");
MySortViewOfCMD textView = new MySortViewOfCMD(getActivity());
textView.setCurrentString(runtimeCmmd[0]);

用此方法得到的文本结果,并不像adb命令行中格式化的,需要自定义控件

  • 自定义view实现排版仅供参考
    public class MySortViewOfCMD extends TextView {

    private String text;
    int lineCount = 0;
    private Paint paint = new Paint();
    private ArrayMap<Integer, String[]> charByMap;
    private int mWidth = 900;// px
    private int mHeight = 660;
    private Scroller mScroller;
    private int lastPointX;
    private int lastPointY;
    public MySortViewOfCMD(Context context) {
        super(context);
        initPaint(context);
    }
    public MySortViewOfCMD(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint(context);
    }
    private void initPaint(Context context) {
        paint.setColor(context.getResources().getColor(R.color.white));
        paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                18, context.getResources().getDisplayMetrics()));
        mScroller = new Scroller(context);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = measureDimension(mWidth, widthMeasureSpec);
        int height = measureDimension(mHeight, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }
    private int measureDimension(int defaultSize, int measureSpec) {

        int result = defaultSize;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(defaultSize, specSize);
        } else {
        result = defaultSize;
        }
        return result;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (int i = 0; i < charByMap.size(); i++) {//绘制行
        String[] strings = charByMap.get(i);
            for (int j = 0; j < strings.length; j++) {//绘制列
                if (j == 1) {//针对第二列做特殊处理,防止与第一列重影
                    canvas.drawText(strings[j], j * (mWidth / 3) + 60,
                            i * 30 + 25, paint);
                continue;
                }
                canvas.drawText(strings[j], j * (mWidth / 3), i * 30 + 25,
                        paint);
            }
        }
        }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            lastPointX = (int) event.getX();
            lastPointY = (int) event.getY();
            return true;
    case MotionEvent.ACTION_MOVE:
            
            int mXMove = (int) event.getX();
             int scrolledX = (int) (lastPointX - mXMove);
     if(getScrollX() + scrolledX < 0){//左边界
                 scrollTo(0, 0);
                 return true;
             }
        if (getScrollX() + getWidth() + scrolledX < (mWidth / 3) * 5)//小于右边界
                mScroller.startScroll(getScrollX(), 0,
                        lastPointX - (int) event.getX(), 0, 200);
            invalidate();

            break;
    case MotionEvent.ACTION_UP:

            break;
        }
        return true;
    }
    /**
     * 平滑滚动        
     */
    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }
    }
        public void setCurrentString(String text) {
        this.text = text;
        charByMap = saveCharByMap(text);
        invalidate();
    }
    /**
     * 根据传进来的string保存每一行的字符
     * 
     * @param string
     *            -显示的字符串 eg: file size use \n filedddd size use \n
     */
     private ArrayMap<Integer, String[]> saveCharByMap(String string) {
        String[] lineString = string.split("\n");
        ArrayMap<Integer, String[]> charMap = new ArrayMap<Integer, String[]>();
        lineCount = lineString.length;
    for (int i = 0; i < lineCount; i++) {
            String[] charItem = lineString[i].split("\\s+");// 按空格切出字符

            charMap.put(i, charItem);
        }
            return charMap;
    }

}

感谢android,感谢开源

相关文章
LVM 缩减 / 根目录导致的开机错误
LVM 缩减 / 根目录导致的开机错误
276 0
|
Windows
拷贝单个文件到U盘提示“目标文件系统 文件过大”,不需格式化解决
拷贝单个文件到U盘提示“目标文件系统 文件过大”,不需格式化解决
894 0
拷贝单个文件到U盘提示“目标文件系统 文件过大”,不需格式化解决
|
1月前
|
Linux Shell 虚拟化
开机自动挂载NTFS分区至Linux:分步指南
在Linux中自动挂载Windows NTFS分区,需创建挂载点(如`/media/c_win`),识别分区(如`/dev/sda1`),获取UUID,并编辑`fstab`文件添加挂载信息。推荐使用UUID以保持稳定性。在VMware环境中可能需添加`force`选项。完成这些步骤后,重启系统,NTFS分区将自动挂载。这对于双系统用户非常方便。
|
2月前
|
存储 弹性计算 运维
自动对磁盘分区、格式化、挂载
【4月更文挑战第29天】
27 1
更改U盘文件系统
U盘默认的文件系统格式为FAT32,存在的问题就是单个文件体积移动复制,不能超过4GB,文件名长度也不可以超过255个字符。
更改U盘文件系统
linxu磁盘分区与格式化和自动挂载
linxu磁盘分区与格式化和自动挂载
176 0
linxu磁盘分区与格式化和自动挂载
U盘装WIN7安装程序无法定位现有分区,也无法创建新的系统分区(转载)
  我最近装WIN7,格了盘之后,总是出现提示“安装程序无法定位现有分区,也无法创建新的系统分区”,想了很多办法,总是无法解决,后来经过多方查找,于昨天晚上终于把系统装上了。     我用U盘装系统,是因为我的光驱读盘不好,有时读不出来,有时又能读出来,所以才想到用U盘来装系统。当然了,你的电脑得支持U盘启动才行,下面开始说处理过程,网上能搜到很多的。现在我把我的处理过程总结如下:
2159 1