Android examples for Hardware:Storage
get Internal Storage Size
/******************************************************************************* * Copyright (c) 2011 MadRobot.//from www . j a va2 s .c o m * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * Contributors: * Elton Kent - initial API and implementation ******************************************************************************/ //package com.java2s; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import android.util.Log; public class Main { private static final String DF_COMMAND = "df"; private static final CharSequence FLASH_FILE_SYSTEM = "yaffs"; private static final String MEMORY_METRIC = "K"; private static final String MOUNT_COMMAND = "mount"; public static final String TAG = "DeviceUtils"; private static final CharSequence UBI_FILE_SYSTEM = "ubifs"; /** * * * @return */ public static int getInternalStorageSize() { String cmd = MOUNT_COMMAND; Runtime run = Runtime.getRuntime(); Process pr = null; /* Parsing Mount Points */ try { pr = run.exec(cmd); } catch (IOException e) { return 0; } try { if (pr != null) { pr.waitFor(); } } catch (InterruptedException e) { return 0; } BufferedReader buf = null; String line = null; ArrayList<String> mountPts = new ArrayList<String>(3); try { buf = new BufferedReader(new InputStreamReader( pr.getInputStream())); while ((line = buf.readLine()) != null) { if (line.contains(FLASH_FILE_SYSTEM) || line.contains(UBI_FILE_SYSTEM)) { StringTokenizer token = new StringTokenizer(line, " "); while (token.hasMoreElements()) { String mntPoint = token.nextToken(); if (mntPoint != null && mntPoint.length() > 0 && !mntPoint.startsWith("/dev/block") && mntPoint.startsWith("/")) { mountPts.add(mntPoint); break; } } } } } catch (IOException e) { Log.e(TAG, "Could not parse mount points!", e); } finally { if (buf != null) try { buf.close(); } catch (IOException e) { } } /* Parsing Mount Point sizes */ cmd = DF_COMMAND; try { pr = run.exec(cmd); } catch (IOException e) { Log.e(TAG, "Could not execute command : " + cmd, e); return 0; } try { if (pr != null) { pr.waitFor(); } } catch (InterruptedException e) { Log.e(TAG, "Could not complete command : " + cmd, e); return 0; } ArrayList<String> mountPtsSizes = new ArrayList<String>( mountPts.size()); try { buf = new BufferedReader(new InputStreamReader( pr.getInputStream())); while ((line = buf.readLine()) != null) { for (String mntPnt : mountPts) { boolean found = false; if (line.contains(mntPnt)) { found = true; mountPts.remove(mntPnt); StringTokenizer token = new StringTokenizer(line, " "); token.nextToken(); while (token.hasMoreElements()) { String size = token.nextToken(); if (size != null && size.length() > 0) { if (size.endsWith("K")) { size = size.substring(0, size.length() - 1); } mountPtsSizes.add(size); break; } } if (found) break; } } if (mountPts.size() == 0) { break; } } } catch (IOException e) { Log.e(TAG, "Could not parse mount point sizes!", e); } finally { if (buf != null) try { buf.close(); } catch (IOException e) { } } /* Summing mount point sizes */ if (mountPtsSizes != null && mountPtsSizes.size() > 0) { Integer fullSize = new Integer(0); for (String size : mountPtsSizes) { fullSize += Integer.parseInt(size); } Log.d(TAG, "Fullsize: " + fullSize + MEMORY_METRIC); return fullSize; } return 0; } }