相关疑难解决方法(0)

Android获得内部/外部内存的免费大小

我想以编程方式获取设备内部/外部存储空闲内存的大小.我正在使用这段代码:

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;
Log.e("","Available MB : "+megAvailable);

File path = Environment.getDataDirectory();
StatFs stat2 = new StatFs(path.getPath());
long blockSize = stat2.getBlockSize();
long availableBlocks = stat2.getAvailableBlocks();
String format =  Formatter.formatFileSize(this, availableBlocks * blockSize);
Log.e("","Format : "+format);
Run Code Online (Sandbox Code Playgroud)

而我得到的结果是:

11-15 10:27:18.844: E/(25822): Available MB : 7572
11-15 10:27:18.844: E/(25822): Format : 869MB
Run Code Online (Sandbox Code Playgroud)

问题是我想获得1,96GB现在的SdCard的免费记忆.如何修复此代码,以便获得免费大小?

android diskspace android-sdcard

89
推荐指数
10
解决办法
8万
查看次数

意图ACTION_DEVICE_STORAGE_LOW的广播时间是什么时候?

在我的应用程序中,我已注册广播接收器以接收系统意图ACTION_DEVICE_STORAGE_LOW.我希望每当手机内存不足时就播放这个内容.所以,我下载了一些额外的应用程序(我的手机有一个非常小的内部存储器),导致操作系统显示系统内存不足的通知.手机剩下10-15 MB.但是,我的广播接收器从未收到过这种意图.然而,系统通知保留在通知栏中,由于内存不足,我无法浏览互联网.

每当显示低内部存储器通知时,是否应该广播此意图?或者是否有一些甚至更低的内存阈值将发送我尚未在手机上播放的广播?在文档中,它只说"广播操作:指示设备上存储器状况不佳的粘性广播".因为它实际上并没有定义"低记忆条件",所以我不知道我做错了什么,或者我还没有达到这个条件.

这是我的BroadCastReceiver的代码:

public class MemoryBroadcastReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
            Log.isExternalStorageProblem = false;
            Log.clearNotification(Log.externalMemoryNotificationID);
        }

        if (action.equals(Intent.ACTION_DEVICE_STORAGE_OK)) {
            Log.isInternalStorageLow = false;
            Log.clearNotification(Log.internalMemoryNotificationID);
        }

        if (action.equals(Intent.ACTION_DEVICE_STORAGE_LOW)) {
            Log.isInternalStorageLow = true;
            Log.displayMemoryNotification("Internal Storage Low",
                    "The internal storage is low, Please fix this.",
                    "Please clear cache and/or uninstall apps.", Log.internalMemoryNotificationID);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一个初始化接收器的服务,添加了intent过滤器并注册它(以及其他内容):

private MemoryBroadcastReceiver memoryBroadcastReciever = new MemoryBroadcastReceiver();

public void registerBroadcastReceiver() {
    IntentFilter filter = new IntentFilter(); …
Run Code Online (Sandbox Code Playgroud)

android broadcastreceiver

10
推荐指数
1
解决办法
9895
查看次数

如何检查设备在 Android Oreo API 26 及更高版本上的存储空间是否不足

如何检查设备在 Android 8 Oreo 上的存储空间是否不足。我在 Android 文档中看到API 26 中不推荐使用Intent.ACTION_DEVICE_STORAGE_LOW

此常量在 API 级别 26 中已弃用。 如果您的应用以 O 或更高版本为目标,则此广播将不再传送到清单中定义的任何 BroadcastReceiver。相反,强烈建议应用程序使用改进的 getCacheDir() 行为,以便系统可以在需要时自动释放存储空间。 - 安卓文档

他们鼓励我改用 getCacheDir()。

但是我对它的了解不多,因为 getCacheDir() 似乎将系统缓存目录路径作为 FILE 对象返回,该对象只能用于清除缓存等。

但我需要检查设备的设备存储空间是否不足。我希望有人能帮助我

android android-intent android-8.0-oreo

5
推荐指数
1
解决办法
3983
查看次数