为什么我会得到不正确的电话和SD卡内存信息? [英] why am I getting incorrect phone and SDcard memory information?

查看:149
本文介绍了为什么我会得到不正确的电话和SD卡内存信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看到从手机设置正确的信息,如SDCART和手机内存。但是看到这个code卡1800 MB的内存总量。我的卡是16GB。无法弄清楚。我已经尝试了很多code,但没有。我该如何解决这个问题。


  

公共静态布尔externalMemoryAvailable(){
          返回android.os.Environment.getExternalStorageState()。等于(
                  android.os.Environment.MEDIA_MOUNTED);
      }

 公共静态字符串getAvailableInternalMemorySize(){
    文件路径= Environment.getDataDirectory();
    体statfs STAT =新体statfs(path.getPath());
    长块大小= stat.getBlockSize();
    长availableBlocks = stat.getAvailableBlocks();
    返回FORMATSIZE(availableBlocks *块大小);
}公共静态字符串getTotalInternalMemorySize(){
    文件路径= Environment.getDataDirectory();
    体statfs STAT =新体statfs(path.getPath());
    长块大小= stat.getBlockSize();
    长totalBlocks = stat.getBlockCount();
    返回FORMATSIZE(totalBlocks *块大小);
}公共静态字符串getAvailableExternalMemorySize(){
    如果(externalMemoryAvailable()){
        文件路径= Environment.getExternalStorageDirectory();
        体statfs STAT =新体statfs(path.getAbsolutePath());
        长块大小= stat.getBlockSize();
        长availableBlocks = stat.getAvailableBlocks();
        返回FORMATSIZE(availableBlocks *块大小);
    }其他{
        返回错误;
    }
}公共静态字符串getTotalExternalMemorySize(){
    如果(externalMemoryAvailable()){
       / *文件路径= Environment.getExternalStorageDirectory();
        体statfs STAT =新体statfs(path.getAbsolutePath());
        长块大小= stat.getBlockSize();
        长totalBlocks = stat.getBlockCount();
        返回FORMATSIZE(totalBlocks *块大小);


  
  

* /
              体statfs STAT =新体statfs(Environment.getExternalStorageDirectory()的getPath());
              长方bytesAvailable =(长)stat.getBlockSize()*(长)stat.getAvailableBlocks();
              长megAvailable =参考bytesAvailable /(1024 * 1024);
              返回FORMATSIZE((长)stat.getBlockSize()*(长)stat.getAvailableBlocks());

 }其他{        返回错误;
    }
}公共静态字符串FORMATSIZE(长尺寸){
    字符串后缀= NULL;    如果(大小> = 1024){
        后缀=KB
        大小/ = 1024;
        如果(大小> = 1024){
            后缀=MB;
            大小/ = 1024;
        }
    }    StringBuilder的ResultBuffer的=新的StringBuilder(Long.toString(大小));    INT commaOffset = resultBuffer.length() - 3;
    而(commaOffset大于0){
        resultBuffer.insert(commaOffset,,);
        commaOffset - = 3;
    }    如果(后缀!= NULL)resultBuffer.append(后缀);
    返回resultBuffer.toString();
}



解决方案

从<一个href=\"http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()\"相对=nofollow>环境#getExternalStorageDirectory()


  

注意:不要用这个词混淆外部在这里。这个目录可以更好地被认为是媒体/共享存储。它是一个可以容纳相对大量的数据的文件系统,并且在所有的应用程序(不强制的权限)共享。传统上,这是一SD卡,但作为内置存储在一个设备,它是从受保护的内部存储不同且可被安装为计算机上的文件系统它也可以被实现。


有两个内部存储。一个用于应用程序和系统,一个用于数据。一个数据就像第一个SD卡。 (这是非常简单化的版本,请见谅。)这也是你通过调用得到一个 getExternalStorageDirectory()方法。

从相同的源


  

在具有多个外部存储目录的设备,该目录重新presents用户将与之交互的主外部存储。进入到二级存储,可通过


我不骗你,这句话结束那里。我发现了另一个回答这是应该帮你列出可用SD卡或外部存储。把它拆开,并使用你所需要的。我认为这将是这个样子:

 字符串外部= System.getenv(SECONDARY_STORAG​​E);
的String [] = externalsArray externals.split(:);
串secondaryExternal = externalsArray [0];
文件路径=新的文件(secondaryExternal);

请注意,我没有测试以上。

I want to see the right information from the phone settings such as in SDCART and phone memory. but see this code card 1800 MB total memory. my card is 16GB. could not figure out. I've tried a lot of code, but did not. How do I solve this problem.

public static boolean externalMemoryAvailable() { return android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); }

public static String getAvailableInternalMemorySize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    return formatSize(availableBlocks * blockSize);
}

public static String getTotalInternalMemorySize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long totalBlocks = stat.getBlockCount();
    return formatSize(totalBlocks * blockSize);
}

public static String getAvailableExternalMemorySize() {
    if (externalMemoryAvailable()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getAbsolutePath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return formatSize(availableBlocks * blockSize);
    } else {
        return ERROR;
    }
}

public static String getTotalExternalMemorySize() {
    if (externalMemoryAvailable()) {
       /* File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getAbsolutePath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return formatSize(totalBlocks * blockSize);

*/ StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); long bytesAvailable = (long) stat.getBlockSize() * (long) stat.getAvailableBlocks(); long megAvailable = bytesAvailable / (1024 * 1024); return formatSize((long) stat.getBlockSize() * (long) stat.getAvailableBlocks());

    } else {

        return ERROR;
    }
}

public static String formatSize(long size) {
    String suffix = null;

    if (size >= 1024) {
        suffix = " KB";
        size /= 1024;
        if (size >= 1024) {
            suffix = " MB";
            size /= 1024;
        }
    }

    StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

    int commaOffset = resultBuffer.length() - 3;
    while (commaOffset > 0) {
        resultBuffer.insert(commaOffset, ',');
        commaOffset -= 3;
    }

    if (suffix != null) resultBuffer.append(suffix);
    return resultBuffer.toString();
}

解决方案

From Environment#getExternalStorageDirectory()

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

There's two internal storages. One for apps and system and one for data. The one for data acts like a first SD card. (This is very dumbed down version, please forgive me.) It's also the one you get by calling the getExternalStorageDirectory() method.

From the same source

In devices with multiple "external" storage directories, this directory represents the "primary" external storage that the user will interact with. Access to secondary storage is available through

I'm not kidding you, the sentence ends there. I found another answer which is supposed to help you list available "SD cards" or external storages. Take it apart and use what you need. I think it will look something like this:

String externals = System.getenv("SECONDARY_STORAGE");
String[] externalsArray = externals.split(":");
String secondaryExternal = externalsArray[0];
File path = new File(secondaryExternal);

Please note that I have not tested the above.

这篇关于为什么我会得到不正确的电话和SD卡内存信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆