解决方案 »

  1. 试下下面的方法
    public String getOutSDPath() {
    String mount = new String(); try {
    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec("mount");
    InputStream is = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    String line; BufferedReader br = new BufferedReader(isr);
    while ((line = br.readLine()) != null) {
    if (line.contains("secure"))
    continue;
    if (line.contains("asec"))
    continue; if (line.contains("fat")) {
    String columns[] = line.split(" ");
    if (columns != null && columns.length > 1) {
    mount = mount.concat("*" + columns[1] + "\n");
    }
    } else if (line.contains("fuse")) {
    String columns[] = line.split(" ");
    if (columns != null && columns.length > 1) {
    mount = mount.concat(columns[1] + "\n");
    }
    }
    }
    Log.i("外置SD卡或者USB设备路径", mount); } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return mount;
    }
      

  2. 代码如下:
    public class MainActivity extends Activity {
            private final String TAG = "MainActivity";        @Override
            protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    TextView internalMemoryTextView = (TextView) findViewById(R.id.rom_left);
                    TextView externalMemoryTextView = (TextView) findViewById(R.id.inside_sd_left);
                    TextView sdCard2MemoryTextView = (TextView) findViewById(R.id.outside_sd_left);
                    internalMemoryTextView.setText("rom left "
                                    + getAvailableInternalMemorySize() + "MB");
                    externalMemoryTextView.setText("rom left "
                                    + getAvailableExternalMemorySize() + "MB");
                    sdCard2MemoryTextView.setText("rom left "
                                    + getAvailableSDCard2MemorySize() + "MB");        }        /**
             * 
             * @return ROM存储路径
             */
            private String getInternalMemoryPath() {
                    return Environment.getDataDirectory().getPath();
            }        /**
             * 
             * @return 内置sd卡路径
             */
            private String getExternalMemoryPath() {
                    // return Environment.getExternalStorageDirectory().getPath();
                    return "/mnt/sdcard";
            }        /**
             * 
             * @return 外置sd卡路径
             */
            private String getSDCard2MemoryPath() {
                    return "/mnt/sdcard1";
            }        /**
             * 
             * @param path
             *            文件路径
             * @return 文件路径的StatFs对象
             * @throws Exception
             *             路径为空或非法异常抛出
             */
            private StatFs getStatFs(String path) {
                    try {
                            return new StatFs(path);
                    } catch (Exception e) {
                            e.printStackTrace();
                    }
                    return null;
            }        /**
             * 
             * @param stat
             *            文件StatFs对象
             * @return 剩余存储空间的MB数
             * 
             */
            private float calculateSizeInMB(StatFs stat) {
                    if (stat != null)
                            return stat.getAvailableBlocks()
                                            * (stat.getBlockSize() / (1024f * 1024f));
                    return 0.0f;
            }        /**
             * 
             * @return ROM剩余存储空间的MB数
             */
            private float getAvailableInternalMemorySize() {                String path = getInternalMemoryPath();// 获取数据目录
                    StatFs stat = getStatFs(path);
                    return calculateSizeInMB(stat);
            }        /**
             * 
             * @return 内置SDCard剩余存储空间MB数
             */
            private float getAvailableExternalMemorySize() {                String path = getExternalMemoryPath();// 获取数据目录
                    StatFs stat = getStatFs(path);
                    return calculateSizeInMB(stat);        }        /**
             * 
             * @return 外置SDCard剩余存储空间MB数
             */
            private float getAvailableSDCard2MemorySize() {                // String status = Environment.getExternalStorageState();
                    // if (status.equals(Environment.MEDIA_MOUNTED)) {
                    // }
                    String path = getSDCard2MemoryPath(); // 获取数据目录
                    StatFs stat = getStatFs(path);
                    return calculateSizeInMB(stat);        }
      

类似问题 »