-1
我想获得外部内存大小。我的代码是在棒棒糖及以下运行完美,但给出棉花糖无效路径异常及以上 我的代码是在这里ANdroid:java.lang.IllegalArgumentException:路径无效:/ storage/extSdCard
public static String getExternalPath(){
String sSDpath = null;
File fileCur = null;
for(String sPathCur : Arrays.asList("MicroSD", "external_SD", "sdcard1", "ext_card", "external_sd", "ext_sd", "external", "extSdCard", "externalSdCard")) // external sdcard
{
fileCur = new File("/mnt/", sPathCur);
if(fileCur.isDirectory() && fileCur.canWrite())
{
sSDpath = fileCur.getAbsolutePath();
break;
}
if(sSDpath == null) {
fileCur = new File("/storage/", sPathCur);
if(fileCur.isDirectory() && fileCur.canWrite())
{
sSDpath = fileCur.getAbsolutePath();
break;
}
}
if(sSDpath == null) {
fileCur = new File("/storage/emulated", sPathCur);
if(fileCur.isDirectory() && fileCur.canWrite())
{
sSDpath = fileCur.getAbsolutePath();
//Log.e("path",sSDpath);
break;
}
}
}
fileCur = new File("/storage/extSdCard");
return fileCur.getAbsolutePath();
}
而且我调用这个函数在我的活动
public static String getExternalTotalMemory() {
StatFs statFs2 = new StatFs(getExternalPath());
long blockSize2 = statFs2.getBlockSize();
final long totalSize2 = statFs2.getBlockCount() * blockSize2;;
return formatSize(totalSize2);
}
你可以把为23+运行时权限,所以你可以参考这个https://developer.android.com/training/permissions/requesting.html –
@HarshitTrivedi我也已经做了这但我得到同样的东西 –
我用这个代码获取权限ActivityCompat.requestPermissions(Main_Booster.this, new String [] {android.Manifest.permission.CLEAR_APP_CACHE,android.Manifest.permission.KILL_BACKGROUND_PROCESSES, android.Manifest。 permission.WRITE_SETTINGS,android.Manifest.permission.BATTERY_STATS,android.Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.READ_EXTERNAL_STORAGE,android.Manifest.permissi on.WRITE_EXTERNAL_STORAGE }, 1); –