2017-09-28 91 views
-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); 
} 
+0

你可以把为23+运行时权限,所以你可以参考这个https://developer.android.com/training/permissions/requesting.html –

+0

@HarshitTrivedi我也已经做了这但我得到同样的东西 –

+0

我用这个代码获取权限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); –

回答

0

在棉花糖及以上版本的外部SD卡的路径是改变,所以它给你错误。

你需要在这个位置搜索你的外部SD卡。

/存储/模拟/ 0