判断SD是否存在及其容量查询

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)、QQ技术交流群(183198395)。

这个非常简单,直接上代码:

MainActivity:

package com.home.getsdinfo;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	private Button getAllBtn;
	private Button getRemainBtn;
	private TextView allText;
	private TextView remainText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initWidget();
	}

	private void initWidget() {
		getAllBtn = (Button) findViewById(R.id.main_btn_all);
		getAllBtn.setOnClickListener(this);
		getRemainBtn = (Button) findViewById(R.id.main_btn_remain);
		getRemainBtn.setOnClickListener(this);
		allText = (TextView) findViewById(R.id.main_tv_all);
		remainText = (TextView) findViewById(R.id.main_tv_remain);
	}

	@Override
	public void onClick(View v) {
		if (v == getAllBtn) {
			if (!isExistSDCard()) {
				Toast.makeText(this, "SD卡不存在!", Toast.LENGTH_SHORT).show();
				return;
			}
			allText.setText(getSDAllSize() + "MB");
		} else if (v == getRemainBtn) {
			if (!isExistSDCard()) {
				Toast.makeText(this, "SD卡不存在!", Toast.LENGTH_SHORT).show();
				return;
			}
			remainText.setText(getSDRemainSize() + "MB");
		}
	}

	/**
	 * 判断SD卡是否存在
	 * 
	 * @return
	 */
	private boolean isExistSDCard() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return true;
		}
		return false;
	}

	/**
	 * 获取SD卡剩余空间
	 * 
	 * @return
	 */
	private long getSDRemainSize() {
		// 获取SD卡根目录文件
		File file = Environment.getExternalStorageDirectory();
		// 构建StatFs对象
		StatFs sfs = new StatFs(file.getPath());
		// 获取单个数据块的大小(Byte)
		long blockSize = sfs.getBlockSize();
		// 获取空闲的数据块的数量
		long freeBlocks = sfs.getAvailableBlocks();
		// 返回SD卡空闲大小
		return (freeBlocks * blockSize) / 1024 / 1024; // 单位MB
	}

	/**
	 * 获取SD卡总容量
	 * 
	 * @return
	 */
	private long getSDAllSize() {
		// 获取SD卡根目录文件
		File file = Environment.getExternalStorageDirectory();
		// 构建StatFs对象
		StatFs sfs = new StatFs(file.getPath());
		// 获取单个数据块的大小(Byte)
		long blockSize = sfs.getBlockSize();
		// 获取所有数据块数
		long allBlocks = sfs.getBlockCount();
		// 返回SD卡大小
		return (allBlocks * blockSize) / 1024 / 1024; // 单位MB
	}
}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/main_btn_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="获取SD卡总容量" />

        <TextView
            android:id="@+id/main_tv_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/main_btn_remain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="获取SD卡剩余空间" />

        <TextView
            android:id="@+id/main_tv_remain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp" />
    </LinearLayout>

</LinearLayout>

权限:

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




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值