动态获取(内存)DDR和(存储)EMCC大小

该代码段展示了如何在Android系统中动态获取DDR(可能是内部存储)的大小。通过执行shell命令读取`/sys/block/mmcblk2/size`来获取闪存大小,并将其转换为GB单位。同时,还提供了一个简化的方法来获取EMMC(外部存储)的总空间,通过直接读取系统目录的总空间。这两个方法都在`StoragePreferenceController`类中实现,用于设置设置界面的存储信息显示。

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

1.动态获取DDR


/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.deviceinfo;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.SWLogger;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.DeviceInfoUtils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class StoragePreferenceController extends BasePreferenceController {

  private static final String TAG = "DeviceModelPrefCtrl";
  private static SWLogger log = SWLogger.getLogger(StoragePreferenceController.class);

  public StoragePreferenceController(Context context, String key) {
    super(context, key);
  }
  @Override
  public void displayPreference(PreferenceScreen screen) {
    super.displayPreference(screen);
  }
  @Override
  public int getAvailabilityStatus() {
    return AVAILABLE;
  }
  @Override
  public CharSequence getSummary() {
    return getStorage();
  }
  public String getStorage() {
    int flashSize = (int) getFlashSize();
    String value = String.valueOf(flashSize) + "GB";
    log.d("getStorage==size==" + value);
    return value;
  }
  private float getFlashSize() {
    Process process = null;
    InputStream inputStream = null;
    InputStreamReader inputStreamReader = null;
    BufferedReader bufferedReader = null;
    float flashSize = 0L;
    try {
      process = Runtime.getRuntime().exec("cat /sys/block/mmcblk2/size");
      inputStream = process.getInputStream();
      byte[] bytes = new byte[1024];
      inputStreamReader = new InputStreamReader(inputStream); //读取
      //创建字符流缓冲区
      bufferedReader = new BufferedReader(inputStreamReader); //从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的
      String line = bufferedReader.readLine();
      if (line != null) {
        long size = Long.parseLong(line);
        log.e("getFlashSize==Size==" + size);
        if (size > 0) {
          flashSize = (float) size / 1024L / 1024L / 2L;
          log.e("getFlashSize==flashSize==" + flashSize);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (process != null) {
          process.destroy();
        }
        if (inputStream != null) {
          inputStream.close();
        }
        if (inputStreamReader != null) {
          inputStreamReader.close();
        }
        if (bufferedReader != null) {
          bufferedReader.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return flashSize = Math.round((float) flashSize) + 1;
  }
  
}

2.获取EMCC大小

方法1:简化实现

long size = roundStorageSize(Environment.getDataDirectory().getTotalSpace()
                + Environment.getRootDirectory().getTotalSpace());

public static long roundStorageSize(long size) {
        long val = 1;
        long pow = 1;
        while ((val * pow) < size) {
            val <<= 1;
            if (val > 512) {
                val = 1;
                pow *= 1000;
            }
        }
        return val * pow;
    }

方法2:


/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.deviceinfo;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.SWLogger;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.DeviceInfoUtils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class StoragePreferenceController extends BasePreferenceController {

  private static final String TAG = "DeviceModelPrefCtrl";
  private static SWLogger log = SWLogger.getLogger(StoragePreferenceController.class);

  public StoragePreferenceController(Context context, String key) {
    super(context, key);
  }
  @Override
  public void displayPreference(PreferenceScreen screen) {
    super.displayPreference(screen);
  }
  @Override
  public int getAvailabilityStatus() {
    return AVAILABLE;
  }
  @Override
  public CharSequence getSummary() {
    return getStorage();
  }
  public String getStorage() {
    int flashSize = (int) getFlashSize();
    String value = String.valueOf(flashSize) + "GB";
    log.d("getStorage==size==" + value);
    return value;
  }
  private float getFlashSize() {
    Process process = null;
    InputStream inputStream = null;
    InputStreamReader inputStreamReader = null;
    BufferedReader bufferedReader = null;
    float flashSize = 0L;
    try {
      process = Runtime.getRuntime().exec("cat /sys/block/mmcblk2/size");
      inputStream = process.getInputStream();
      byte[] bytes = new byte[1024];
      inputStreamReader = new InputStreamReader(inputStream); //读取
      //创建字符流缓冲区
      bufferedReader = new BufferedReader(inputStreamReader); //从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的
      String line = bufferedReader.readLine();
      if (line != null) {
        long size = Long.parseLong(line);
        log.e("getFlashSize==Size==" + size);
        if (size > 0) {
          flashSize = (float) size / 1024L / 1024L / 2L;
          log.e("getFlashSize==flashSize==" + flashSize);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (process != null) {
          process.destroy();
        }
        if (inputStream != null) {
          inputStream.close();
        }
        if (inputStreamReader != null) {
          inputStreamReader.close();
        }
        if (bufferedReader != null) {
          bufferedReader.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return flashSize = Math.round((float) flashSize) + 1;
  }
  
}

04-03
### 关于 `emcc` 的使用教程 #### 基本概念 `emcc` 是 Emscripten 编译器前端的核心工具,用于将 C C++ 代码编译为目标平台上的 JavaScript 或 WebAssembly 文件。它是 Emscripten 工具链中的主要接口[^3]。 --- #### 安装与配置 在使用 `emcc` 之前,需完成环境搭建。以下是基本步骤: 1. 下载并安装 Emscripten SDK。 2. 初始化 SDK 并更新至最新版本。 3. 设置环境变量以便调用 `emcc`。 具体操作可参考官方文档或相关指南。 --- #### 命令语法 `emcc` 支持多种选项参数,其通用形式如下: ```bash emcc [input files...] [options...] -o output_file ``` 其中: - `[input files...]`: 输入文件路径(通常是 `.c`, `.cpp` 等源码文件)。 - `[options...]`: 各种编译选项。 - `-o output_file`: 输出目标文件名。 --- #### 示例:C/C++ 到 WebAssembly 的转换 ##### 将 C 代码编译为 JavaScript 假设有一个名为 `hello_world.c` 的简单程序,可以执行以下命令将其编译为 JavaScript 文件: ```bash emcc hello_world.c -o hello_world.js ``` 此命令会生成两个文件:`hello_world.js` `hello_world.wasm`[^1]。 ##### 将 C++ 代码编译为 WebAssembly 模块 对于更复杂的场景,比如需要优化性能或将 C++ 转换为 WebAssembly 模块时,可以使用以下命令: ```bash emcc main.cpp -s WASM=1 -O3 -o add.js ``` 上述命令中: - `-s WASM=1` 表示启用 WebAssembly 输出模式。 - `-O3` 表示最高级别的优化级别。 - `-o add.js` 指定输出文件名称[^2]。 --- #### 高级功能 除了基础编译外,`emcc` 还支持许多高级特性,例如链接外部库、调整内存布局以及定制化构建行为等。例如,在处理多媒体应用时,可能需要用到 FFmpeg 库的支持,可以通过以下方式实现: ```bash emcc input.c --bind -s USE_FFMPEG=1 -o output.js ``` 这里的关键在于指定 `--bind` 参数以允许绑定到 JavaScript 中,并通过设置标志位激活特定的功能模块[^4]。 --- #### 错误排查技巧 当遇到错误时,可以从以下几个方面入手解决问题: 1. **检查依赖项**: 确保所有必要的头文件库均已正确加载。 2. **验证输入格式**: 只能接受标准的 C/C++ 源代码作为输入。 3. **查阅日志信息**: 如果失败,则查看详细的错误消息来定位问题所在位置。 4. **升级工具链**: 对旧版可能存在兼容性缺陷的情况考虑及时更新至新版本。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值