android获取手机内部存储空间和外部存储空间

原文:<wbr style="line-height:25px"><a href="http://blog.163.com/hero_213/blog/static/39891214201162123236660/" style="color:rgb(245,149,19); line-height:25px; text-decoration:none">http://blog.163.com/hero_213/blog/static/39891214201162123236660/</a> <div style="line-height:25px"> <div style="line-height:25px">该代码片段可以让我们获取internal和external的存储空间大小。</div> <div style="line-height:25px"> <pre class="prettyprint" style="line-height:25px; padding-top:2px; padding-right:2px; padding-bottom:2px; padding-left:2px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(136,136,136); border-right-color:rgb(136,136,136); border-bottom-color:rgb(136,136,136); border-left-color:rgb(136,136,136)"><p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"></p><div style="line-height:25px"> <span class="kwd" style="line-height:25px; color:rgb(0,0,136)">import</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> java</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">io</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">File</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">;</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span> </div><div style="line-height:25px"> <span class="kwd" style="line-height:25px; color:rgb(0,0,136)">import</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> android</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">os</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">Environment</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">;</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span> </div><div style="line-height:25px"> <span class="kwd" style="line-height:25px; color:rgb(0,0,136)">import</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> android</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">os</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">StatFs</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">;</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span> </div><div style="line-height:25px"><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span></div><div style="line-height:25px"> <span class="kwd" style="line-height:25px; color:rgb(0,0,136)">public</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">class</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">StorageUtil</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">{</span> </div><div style="line-height:25px"><br style="line-height:25px"></div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">private</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">static</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">final</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">int</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> ERROR </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">-</span><span class="lit" style="line-height:25px; color:rgb(0,102,102)">1</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">;</span> </div><div style="line-height:25px"><br style="line-height:25px"></div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="com" style="line-height:25px; color:rgb(136,0,0)">/**</span> </div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> * SDCARD是否存</span></div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> */</span></div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">public</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">static</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">boolean</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> externalMemoryAvailable</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">()</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">{</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">return</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> android</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">os</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">Environment</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getExternalStorageState</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">().</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">equals</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">(</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> android</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">os</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">Environment</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">MEDIA_MOUNTED</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">);</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">}</span> </div><div style="line-height:25px"><br style="line-height:25px"></div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="com" style="line-height:25px; color:rgb(136,0,0)">/**</span> </div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> * 获取手机内部剩余存储空间</span></div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> * @return</span></div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> */</span></div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">public</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">static</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">long</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> getAvailableInternalMemorySize</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">()</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">{</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">File</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> path </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">Environment</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getDataDirectory</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">();</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">StatFs</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> stat </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">new</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">StatFs</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">path</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getPath</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">());</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">long</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> blockSize </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> stat</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getBlockSize</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">();</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">long</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> availableBlocks </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> stat</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getAvailableBlocks</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">();</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">return</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> availableBlocks </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">*</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> blockSize</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">;</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">}</span> </div><div style="line-height:25px"><br style="line-height:25px"></div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="com" style="line-height:25px; color:rgb(136,0,0)">/**</span> </div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> * 获取手机内部总的存储空间</span></div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> * @return</span></div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> */</span></div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">public</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">static</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">long</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> getTotalInternalMemorySize</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">()</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">{</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">File</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> path </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">Environment</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getDataDirectory</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">();</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">StatFs</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> stat </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">new</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">StatFs</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">path</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getPath</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">());</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">long</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> blockSize </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> stat</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getBlockSize</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">();</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">long</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> totalBlocks </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> stat</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getBlockCount</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">();</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">return</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> totalBlocks </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">*</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> blockSize</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">;</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">}</span> </div><div style="line-height:25px"><br style="line-height:25px"></div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="com" style="line-height:25px; color:rgb(136,0,0)">/**</span> </div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> * 获取SDCARD剩余存储空间</span></div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> * @return</span></div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> */</span></div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">public</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">static</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">long</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> getAvailableExternalMemorySize</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">()</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">{</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">if</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">externalMemoryAvailable</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">())</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">{</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">File</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> path </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">Environment</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getExternalStorageDirectory</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">();</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">StatFs</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> stat </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">new</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">StatFs</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">path</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getPath</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">());</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">long</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> blockSize </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> stat</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getBlockSize</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">();</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">long</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> availableBlocks </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> stat</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getAvailableBlocks</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">();</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">return</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> availableBlocks </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">*</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> blockSize</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">;</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">}</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">else</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">{</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">return</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> ERROR</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">;</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">}</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">}</span> </div><div style="line-height:25px"><br style="line-height:25px"></div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="com" style="line-height:25px; color:rgb(136,0,0)">/**</span> </div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> * 获取SDCARD总的存储空间</span></div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> * @return</span></div><div style="line-height:25px"><span class="com" style="line-height:25px; color:rgb(136,0,0)"> */</span></div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">public</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">static</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">long</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> getTotalExternalMemorySize</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">()</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">{</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">if</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">externalMemoryAvailable</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">())</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">{</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">File</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> path </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">Environment</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getExternalStorageDirectory</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">();</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">StatFs</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> stat </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">new</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:25px; color:rgb(102,0,102)">StatFs</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">path</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getPath</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">());</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">long</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> blockSize </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> stat</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getBlockSize</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">();</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">long</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> totalBlocks </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> stat</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)">getBlockCount</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">();</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">return</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> totalBlocks </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">*</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> blockSize</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">;</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">}</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">else</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">{</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:25px; color:rgb(0,0,136)">return</span><span class="pln" style="line-height:25px; color:rgb(0,0,0)"> ERROR</span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">;</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">}</span> </div><div style="line-height:25px"> <span class="pln" style="line-height:25px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:25px; color:rgb(102,102,0)">}</span> </div><div style="line-height:25px"><span class="pun" style="line-height:25px; color:rgb(102,102,0)">}</span></div><p style="line-height:25px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"></p></pre> </div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">1</span>.硬件上的 block size, 应该是"sector size",linux的扇区大小是512byte</div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">2</span>.有文件系统的分区的block size, 是"block size",大小不一,可以用工具查看</div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">3</span>.没有文件系统的分区的block size,也叫“block size”,大小指的是1024 byte</div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">4</span>.Kernel buffer cache 的block size, 就是"block size",大部分PC是1024</div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">5</span>.磁盘分区的"cylinder size",用fdisk 可以查看。</div> <div style="line-height:25px">我们这里的block size是第二种情况,一般SD卡都是fat32的文件系统,block size是4096.</div> <div style="line-height:25px">这样就可以知道手机的内部存储空间和sd卡存储空间的总大小和可用大小了。</div> <div><br></div> </div> </wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值