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

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;
  }
  
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值