android sd卡状态、路径、可用空间,内存

sd卡状态、路径、可用空间byte,参考http://ai4work.blog.163.com/blog/static/189873306201182310304586/

内存,参考http://blog.csdn.net/hudashi/article/details/7050897

manifest权限:

<!-- 在SDCard中创建与删除文件权限 -->
	<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
	<!-- 往SDCard写入数据权限 -->
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


code:

package com.test;

import android.app.Activity;
import android.app.ActivityManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

public class Test2 extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		int i = 0;
		while (i < 1) {
			try {
				System.out.println("1: " + checkSDCard());
				String path = getExternalStoragePath();
				System.out.println("2: " + path);
				if (path != null) {
					System.out.println("3: " + (getAvailableStore(path)>>20) + "MB");
				}
				displayBriefMemory();
			} catch (Exception e) {
				e.printStackTrace();
			}
			i++;
		}

	}

	//内存相关
	private void displayBriefMemory() {    
        final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);    
        ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();   
        activityManager.getMemoryInfo(info);    
        Log.i("System.out", "系统剩余内存:"+(info.availMem >> 20)+"MB");   
        Log.i("System.out", "系统是否处于低内存运行:"+info.lowMemory);
        Log.i("System.out", "当系统剩余内存低于" + (info.threshold >> 20) + "MB时就看成低内存运行");
    } 
	
	// 检查SD卡状态
	private boolean checkSDCard() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return true;
		}
		return false;
	}

	// 获取SD卡路径
	public static String getExternalStoragePath() {
		// 获取SdCard状态
		String state = android.os.Environment.getExternalStorageState();

		// 判断SdCard是否存在并且是可用的

		if (android.os.Environment.MEDIA_MOUNTED.equals(state)) {

			if (android.os.Environment.getExternalStorageDirectory().canWrite()) {

				return android.os.Environment.getExternalStorageDirectory()
						.getPath();

			}

		}

		return null;

	}

	/**
	 *   * 获取存储卡的剩余容量,单位为字节
	 * 
	 *   * @param filePath
	 * 
	 *   * @return availableSpare
	 * 
	 *   
	 */

	public static long getAvailableStore(String filePath) {

		// 取得sdcard文件路径

		StatFs statFs = new StatFs(filePath);

		// 获取block的SIZE

		long blocSize = statFs.getBlockSize();

		// 获取BLOCK数量

		// long totalBlocks = statFs.getBlockCount();

		// 可使用的Block的数量

		long availaBlock = statFs.getAvailableBlocks();

		// long total = totalBlocks * blocSize;

		long availableSpare = availaBlock * blocSize;

		return availableSpare;

	}

}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值