云主机测评网云主机测评网云主机测评网

云主机测评网
www.yunzhuji.net

Android如何判断设备是否有多个存储分区?

Android通过Environment.getExternalStorageState()判断多个存储,如返回MEDIA_SHARED表示共享存储。

Android判断多个存储

背景介绍

在Android开发中,存储管理是一个重要的方面,随着应用程序生成的数据量不断增加,了解设备的存储情况变得至关重要,Android设备通常提供多种存储选项,包括内部存储、外部存储(如SD卡)等,本文将探讨如何在Android应用中判断多个存储路径及其可用性。

一、获取内置存储信息

内部存储空间总大小

使用StatFs类来获取内部存储的总大小:

import android.os.Environment;
import android.os.StatFs;
public class StorageUtil {
    public static long getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long totalBlocks = stat.getBlockCountLong();
        return totalBlocks * blockSize;
    }
}

内部存储剩余空间

同样使用StatFs类来获取内部存储的剩余空间:

public class StorageUtil {
    public static long getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long availableBlocks = stat.getAvailableBlocksLong();
        return availableBlocks * blockSize;
    }
}

二、获取外置SD卡信息

判断SD卡是否可用

通过Environment类的getExternalStorageState()方法来判断SD卡是否挂载:

public class SdCardUtil {
    public static boolean isSDCardEnable() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }
}

获取SD卡路径

获取外部存储的根目录路径:

public class SdCardUtil {
    public static String getSDCardPath() {
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }
}

获取SD卡总空间大小和可用空间大小

使用StatFs类获取SD卡的总大小和可用大小:

public class SdCardUtil {
    public static long getTotalExternalMemorySize() {
        if (isSDCardEnable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSizeLong();
            long totalBlocks = stat.getBlockCountLong();
            return totalBlocks * blockSize;
        } else {
            return -1;
        }
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    public static String getFreeSpace() {
        if (!isSDCardEnable()) return "sdcard unable!";
        StatFs stat = new StatFs(getSDCardPath());
        long blockSize, availableBlocks;
        availableBlocks = stat.getAvailableBlocksLong();
        blockSize = stat.getBlockSizeLong();
        long size = availableBlocks * blockSize / 1024L;
        return String.valueOf(size);
    }
}

三、获取所有存储路径

有时设备可能有多张SD卡,需要获取所有可用的存储路径:

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class SdCardUtil {
    public static List<String> getExtSDCardPathList() {
        List<String> paths = new ArrayList<>();
        String extFileStatus = Environment.getExternalStorageState();
        File extFile = Environment.getExternalStorageDirectory();
        if (extFileStatus.equals(Environment.MEDIA_MOUNTED) && extFile.exists() && extFile.isDirectory() && extFile.canWrite()) {
            paths.add(extFile.getAbsolutePath());
        }
        try {
            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;
            int mountPathIndex = 1;
            while ((line = br.readLine()) != null) {
                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() || !mountRoot.canWrite()) {
                    continue;
                }
                boolean equalsToPrimarySD = mountPath.equals(extFile.getAbsolutePath());
                if (equalsToPrimarySD) {
                    continue;
                }
                paths.add(mountPath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return paths;
    }
}

四、归纳

本文详细介绍了如何在Android应用中判断多个存储路径及其可用性,包括内部存储和外部存储(SD卡),通过使用Environment类和StatFs类,开发者可以轻松获取存储设备的总大小和可用空间,还提供了如何判断SD卡是否可用以及获取所有存储路径的方法,这些信息对于需要进行文件管理和存储优化的应用非常有用。

打赏
版权声明:主机测评不销售、不代购、不提供任何支持,仅分享信息/测评(有时效性),自行辨别,请遵纪守法文明上网。
文章名称:《Android如何判断设备是否有多个存储分区?》
文章链接:https://www.yunzhuji.net/wangzhanyunwei/135623.html

评论

  • 验证码