获取安卓内存状态

获取安卓内存状态_第1张图片


package com.itheiima28.memorydemo;

 
import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		TextView tvMemoryInfo = (TextView) findViewById(R.id.tv_memory_info);
		
		//获得sd卡的内存状态
		File sdcardFileDir = Environment.getExternalStorageDirectory();
		System.out.println(sdcardFileDir.toString());
		String sdcardMemory = getMemoryInfo(sdcardFileDir);
		
		//获得手机内部存储控件的状态
		File dataFileDir = Environment.getDataDirectory();//   /data
		String dataMemory = getMemoryInfo(dataFileDir);
		//System.out.println(dataFileDir.toString());
		tvMemoryInfo.setText("SD卡:"+sdcardMemory + "\n手机内部内存:"+dataMemory);
	}
	
	/**
	 * 根据路径获取内存的状态
	 * */
	public String getMemoryInfo(File path){
		//获得一个磁盘状态对象
		//File path = Environment.getExternalStorageDirectory();
		
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();//获得一个扇区的大小
        long totalBlocks = stat.getBlockCount(); //获得一个扇区的总数
        long availableBlocks = stat.getAvailableBlocks();//获得可用的扇区
        
        
        //总空间
        String totalMemory = Formatter.formatFileSize(this, totalBlocks*blockSize);
        //可用空间
        String availableMemory = Formatter.formatFileSize(this, availableBlocks*blockSize);
        
        return "总空间:" 	+ totalMemory + "\n" + "可用空间:" + availableMemory;
	}
}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.itheiima28.memorydemo.MainActivity" >

    <TextView
        android:id="@+id/tv_memory_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"  />

</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheiima28.memorydemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




关于找内存空间源码:  在安卓Total space

获取安卓内存状态_第2张图片


查找结果:

获取安卓内存状态_第3张图片


进入之后再次查找





再次搜索:memory_sd_size



获取安卓内存状态_第4张图片




Memory.java中的有关代码:




   <span style="white-space:pre">		</span> File path = Environment.getExternalStorageDirectory();
                StatFs stat = new StatFs(path.getPath());
                long blockSize = stat.getBlockSize();
                long totalBlocks = stat.getBlockCount();
                long availableBlocks = stat.getAvailableBlocks();
                
                mSdSize.setSummary(formatSize(totalBlocks * blockSize));
                mSdAvail.setSummary(formatSize(availableBlocks * blockSize) + readOnly);

                mSdMountToggle.setEnabled(true);
                mSdMountToggle.setTitle(mRes.getString(R.string.sd_eject));
                mSdMountToggle.setSummary(mRes.getString(R.string.sd_eject_summary));


你可能感兴趣的:(android)