2017-02-17 50 views
0

我正在开发一个android文件管理器应用程序。 等主要活动我想显示所有可用的存储类型,如内部存储和外部SD卡。获取内部和外部存储信息 - android

,所以我用这个代码,

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

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

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

public static long getAvailableExternalMemorySize() { 
    if (externalMemoryAvailable()) { 
     File path = Environment.getExternalStorageDirectory(); 

     StatFs stat = new StatFs(path.getPath()); 
     long blockSize = stat.getBlockSize(); 
     long availableBlocks = stat.getAvailableBlocks(); 
     return availableBlocks * blockSize; 
    } else { 
     return 0; 
    } 
} 

public static long getTotalExternalMemorySize() { 
    if (externalMemoryAvailable()) { 
     File path = Environment.getExternalStorageDirectory(); 
     StatFs stat = new StatFs(path.getPath()); 
     long blockSize = stat.getBlockSize(); 
     long totalBlocks = stat.getBlockCount(); 
     return totalBlocks * blockSize; 
    } else { 
     return 0; 
    } 
} 

但问题是,它给我相同的内存输出,用于内部和外部存储.. 实际上它给内部存储正确的答案。但外部SD卡错误。

我觉得我错在获得ext sd卡的路径。任何帮助? PLZ。

+0

'但错误的外置SD card'。确实。您的代码中没有涉及外部SD卡的内容。 – greenapps

+0

'它为我提供了同样的内存和外部存储输出.'。有趣。确认! – greenapps

回答

1

是SD卡位置路径是android的不同品牌不同,不能得到保证。 我有一个解决方案,但这个工程用的minSdkVersion 19

static File dirs[]; 
dirs = ContextCompat.getExternalFilesDirs(context, null); 
//dirs[0] refers to internal memory and dirs[1] gives you external. Call the following methods to get total and available memory details. 


public static String getTotalExternalMemorySize(File dirs[]) { 
if (dirs.length > 1) { 
     StatFs stat = new StatFs(dirs[1].getPath()); 
     long blockSize = stat.getBlockSizeLong(); 
     long totalBlocks = stat.getBlockCountLong(); 
     return readableFileSize(totalBlocks * blockSize); 
    } else { 
     return "NA"; 
} 

public static String getAvailableExternalMemorySize(File[] dirs) { 
    if (dirs.length > 1) { 
     StatFs stat = new StatFs(dirs[1].getPath()); 
     long blockSize = stat.getBlockSizeLong(); 
     long availableBlocks = stat.getAvailableBlocksLong(); 
     return readableFileSize(availableBlocks * blockSize); 
    } else { 
     return "NA"; 
    } 
} 

public static String readableFileSize(long size) { 
    if(size <= 0) return "0"; 
    final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" }; 
    int digitGroups = (int) (Math.log10(size)/Math.log10(1024)); 
    return new DecimalFormat("#,##0.##").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups]; 
} 
+0

thanx @pavan。但你会告诉我,我怎么能检测到另一个USB存储。意味着如果手机已经将USB笔驱动器连接到了otg,该怎么办。那么我如何在这里检测到它? –

+0

意味着如果,手机已插入SD卡,以及pendrive连接? –

+0

您是否尝试循环dirs []数组以检查它是否返回了所有外部坐骑? 根据文档** getExternalFilesDirs(context,type)**方法执行以下操作: **返回应用程序可以放置它拥有的持久性文件的所有外部存储设备上应用程序特定的目录的绝对路径** – Pavan