工具类 SDCardUtils

本文详细介绍了如何使用Java编写一个工具类来获取手机上的SD卡路径,并提供了获取SD卡剩余空间大小和总容量的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<pre name="code" class="java">package com.example.helloworld;

import android.os.Environment;
import android.os.StatFs;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * 获取手机sd卡的工具类
 * 
 * @author
 */
public class SDCardUtils {
    /*
     * avoid initializations of tool classes
     */
    private SDCardUtils() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @Title: getExtSDCardPaths
     * @Description: to obtain storage paths, the first path is theoretically
     *               the returned value of
     *               Environment.getExternalStorageDirectory(), namely the
     *               primary external storage. It can be the storage of internal
     *               device, or that of external sdcard. If paths.size() >1,
     *               basically, the current device contains two type of storage:
     *               one is the storage of the device itself, one is that of
     *               external sdcard. Additionally, the paths is directory.
     * @return List<String>
     * @throws IOException 获取手机上所有可用的sd卡路径
     * @return
     */
    public static ArrayList<String> getExtSDCardPaths() {
        ArrayList<String> paths = new ArrayList<String>();
        String extFileStatus = Environment.getExternalStorageState();
        File extFile = Environment.getExternalStorageDirectory();
        if (extFileStatus.equals(Environment.MEDIA_MOUNTED) && extFile.exists()
                && extFile.isDirectory()) {
            paths.add(extFile.getAbsolutePath());
        }
        try {
            // obtain executed result of command line code of 'mount', to judge
            // whether tfCard exists by the result
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("mount");
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            int mountPathIndex = 1;
            while ((line = br.readLine()) != null) {
                // format of sdcard file system: vfat/fuse
                if ((!line.contains("fat") && !line.contains("fuse") && !line.contains("storage"))
                        || line.contains("secure") || line.contains("asec")
                        || line.contains("firmware") || line.contains("shell")
                        || line.contains("obb") || line.contains("legacy") || line.contains("data")) {
                    continue;
                }
                String[] parts = line.split(" ");
                int length = parts.length;
                if (mountPathIndex >= length) {
                    continue;
                }
                String mountPath = parts[mountPathIndex];
                if (!mountPath.contains("/") || mountPath.contains("data")
                        || mountPath.contains("Data")) {
                    continue;
                }
                File mountRoot = new File(mountPath);
                if (!mountRoot.exists() || !mountRoot.isDirectory()) {
                    continue;
                }
                boolean equalsToPrimarySD = mountPath.equals(extFile.getAbsolutePath());
                if (equalsToPrimarySD) {
                    continue;
                }
                paths.add(mountPath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return paths;
    }

    /**
     * SD卡剩余空间大小
     * 
     * @param path 取得SD卡文件路径
     * @return 单位MB
     */
    public static long getSDFreeSize(String path) {
        StatFs sf = new StatFs(path);
        // 获取单个数据块的大小(Byte)
        long blockSize = sf.getBlockSize();
        // 空闲的数据块的数量
        long freeBlocks = sf.getAvailableBlocks();
        // 返回SD卡空闲大小
        return (freeBlocks * blockSize) / 1024 / 1024; //
    }

    /**
     * SD卡总容量
     * 
     * @param path 取得SD卡文件路径
     * @return 单位MB
     */
    public static long getSDAllSize(String path) {
        StatFs sf = new StatFs(path);
        // 获取单个数据块的大小(Byte)
        long blockSize = sf.getBlockSize();
        // 获取所有数据块数
        long allBlocks = sf.getBlockCount();
        // 返回SD卡大小
        // return allBlocks * blockSize; //单位Byte
        // return (allBlocks * blockSize)/1024; //单位KB
        return (allBlocks * blockSize) / 1024 / 1024;
    }

    /**
     * 判断当前内存卡是否可用
     * 
     * @param mContext
     * @return
     */
    public static final boolean isExist(String sdPath) {
        ArrayList<String> list = getExtSDCardPaths();
        for (int i = 0; i < list.size(); i++) {
            if (list.contains(sdPath)) {
                return true;
            }
        }
        return false;
    }
}




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值