0%

代码中获得系统分区

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

我们用如下方法可执行任意的adb shell命令,在此仅用”df”举例
  • 代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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();
}
}
}

我们在代码中按如下使用

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

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

  • 自定义view实现排版仅供参考
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    	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,感谢开源