`
java-mans
  • 浏览: 11429840 次
文章分类
社区版块
存档分类
最新评论

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>
分享到:
评论

相关推荐

    Android获取手机所有文件存储路径

    博客地址:http://blog.csdn.net/lowprofile_coding/article/details/48269011 1.获取扩展存储设备 2.获取sdcard2外部存储空间 3.获取可用的 EMMC 内部存储空间 4.获取其他外部存储可用空间 5.获取内部存储目录

    Android之获取手机内部及sdcard存储空间的方法

    内部存储空间获取总大小和可用大小; sdcard存储空间获取总大小和可用大小; 新名词记录{StatFs:描述文件系统信息的类} 概览 在开发中,我们或多或少都会用到存储空间进行存储数据。移动设备上可以有两种的存储方式...

    Android-存储路径与管理应用文件存储方法汇总

     Android手机存储分为两个部分:内部存储和外部存储,内部存储一般是手机自带的存储空间,外部存储指外插SD卡提供的存储空间;随着手机发展,这两个存储的定义又有了一些些变化,新的手机不再有外插SD卡的概念,...

    Android文件存储:内部存储,外部存储及其路径

    Android系统数据存储分为内部存储和外部存储,内部存储是手机系统自带的存储,一般空间都不大,外部存储一般是SD卡的存储,空间一般都比较大。一般我们会把存储内容放在外部存储空间里。在使用过程需要注意事项: 先...

    数据存储之——Android内、外存储分区&常用存储目录详解(Android Q)

    这些名称是在Android早期确定的,那时候大部分设备都提供内置的非易失性内存(内部存储空间)以及可移动存储媒介(如,Micro SD卡,提供外部存储空间)。现在,很多设备将永久性存储空间划分为单独的“内部”和“外部...

    Android App将数据写入内部存储和外部存储的示例

    一旦程序在设备安装后,data/data/包名/ 即为内部存储空间,对外保密。 Context提供了2个方法来打开输入、输出流 FileInputStream openFileInput(String name) FileOutputStream openFileOutput(String name, int ...

    StorageHelper-for-Android:获取设备上的所有存储空间,包括 USB 设备

    获取设备上的所有存储空间,包括 USB 设备 此类使用当前的 Android API 检索所有存储。 要从复制话: 内部存储在 API 中称为“外部存储”。 如环境文档中所述: 注意:不要被这里的“外部”一词混淆。 这个目录...

    Android基础 布局、数据存储访问、XML系列化解析和SharedPreferences入门

    主要介绍存储文件到外部存储器和内部存储器,利用系统提供的API获取路径时,需要精准的掌握他们的目录层级。在将数据保存到SDCard时,需要判断剩余存储空间。 SharedPreferences存储对于简单的键值对形式非常方便...

    详解Android文件存储

    Android存储空间包括内部存储空间(Internal Storage)和外部存储空间(External Storage),本文分别对Android内部存储空间(Internal Storage)和Android外部存储空间(External Storage)做了详细讲解

    Android 8.0 读取内部和外部存储以及外置SDcard的方法

    一般的Android App能读取的存储空间主要有三种: app自己的私有目录,也就是/data/data/《app 目录》。 读写这个目录不需要单独的权限。每个app只能读写自己的目录,而不能读写其他app的目录。 Android通过Seandroid...

    Android中Glide获取缓存大小并清除缓存图片

    清除Glide缓存 Glide自带清除缓存的功能,分别对应Glide.get(context).clearDiskCache();(清除磁盘缓存)与Glide.get...一下方法适合在Glide为默认的缓存目录的情况,不论是内部存储空间还是外部.因为我们可以通过Inte

    Android 基础笔记 04 篇:数据存储篇

    应用的专属空间存放文件的目录:内部存储目录和外部存储目录。 应用的专属空间存放的文件类型:持久文件和缓存文件. 应用不需要任何权限就可以对专属空间中的文件进行读写,而其他应用不允许访问这些文件。 应用...

    新版Android开发教程.rar

    谷歌早在 2002 年就进入了移动领域,可是由于目前的手机操作系统企业和手机企业相对封闭,提高了 行业的进入门槛,移动互联网的发展远没有拥有统一标准的传统互联网发展迅速,此次推出的开源手机操 作 系统平台就是...

    深入理解Android:卷I--详细书签版

     第9章分析了Vold和Rild,其中Vold负责Android平台中外部存储设备的管理,而Rild负责与射频通信有关的工作。本章的拓展思考部分介绍了嵌入式系统中与存储有关的知识,还探讨了 Rild和Phone设计优化方面的问题。 ...

    Android知识点及重要代码合集 word文档

    13.5外部存储 176 14.1 SQL的分类 184 14.2常用SQL语句的使用 184 14.3数据库的使用(SQL) 185 14.4 数据库的使用(调用已封装SQL语句的方法) 200 14.5 简单游标适配器的使用及分页效果的实现 207 15.1对手机通讯录的...

    Android应用源码Mezzofanti文字扫描识别

    requested:0[MB] free:-1040[MB] 没有足够的空间用于外部安装的存储要求:0[MB]免费:-1040[MB],实际情况是我的手机储存还有6个G,SD卡也有3个G。看了一下项目主页也有人反映这个问题,不过我没有找到解决方法。两...

Global site tag (gtag.js) - Google Analytics