Android: 获取一个空的MediaStore存储/卷的可用空间?
创始人
2024-10-04 00:17:15
0

您可以使用以下代码示例来获取Android设备上一个空的MediaStore存储/卷的可用空间:

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.documentfile.provider.DocumentFile;

public class StorageUtils {

    private static final String TAG = StorageUtils.class.getSimpleName();

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public static long getAvailableSpace(Context context) {
        Uri externalUri = MediaStore.Files.getContentUri("external");

        // Get the volume name of the primary external storage
        String volumeName = getPrimaryVolumeName(context, externalUri);

        if (volumeName == null) {
            Log.e(TAG, "Failed to get primary volume name");
            return 0;
        }

        // Get the document tree Uri of the primary external storage
        Uri documentTreeUri = DocumentsContract.buildTreeDocumentUri(externalUri.getAuthority(), volumeName);

        if (documentTreeUri == null) {
            Log.e(TAG, "Failed to get document tree Uri");
            return 0;
        }

        // Get the available space of the primary external storage
        DocumentFile documentFile = DocumentFile.fromTreeUri(context, documentTreeUri);

        if (documentFile == null) {
            Log.e(TAG, "Failed to get document file");
            return 0;
        }

        return documentFile.getFreeSpace();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private static String getPrimaryVolumeName(Context context, Uri externalUri) {
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(externalUri, new String[]{MediaStore.VOLUME_NAME}, null, null, null);

            if (cursor != null && cursor.moveToFirst()) {
                return cursor.getString(cursor.getColumnIndex(MediaStore.VOLUME_NAME));
            }
        } catch (Exception e) {
            Log.e(TAG, "Failed to get primary volume name", e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        return null;
    }
}

要使用上面的代码示例,您需要在AndroidManifest.xml文件中添加以下权限:


然后,您可以在您的Activity或Fragment中调用以下方法来获取可用空间:

long availableSpace = StorageUtils.getAvailableSpace(this);

请注意,此示例仅适用于Android 5.0(API级别21)及更高版本。对于更旧的Android版本,您可能需要使用其他方法来获取可用空间。

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...