ANdroid:java.lang.IllegalArgumentException:无效路径:/storage/extSdCard
我正在尝试获取外部内存大小。我的代码在棒棒糖和更低版本上运行得很好,但在棉花糖和更高版本上出现了无效路径的异常 我的密码在这里ANdroid:java.lang.IllegalArgumentException:无效路径:/storage/extSdCard,java,android,exception,android-external-storage,Java,Android,Exception,Android External Storage,我正在尝试获取外部内存大小。我的代码在棒棒糖和更低版本上运行得很好,但在棉花糖和更高版本上出现了无效路径的异常 我的密码在这里 public static String getExternalPath(){ String sSDpath = null; File fileCur = null; for( String sPathCur : Arrays.asList("MicroSD", "external_SD", "sdcard1", "ext_card",
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);
}
在棉花糖和更高版本中,外部SD卡的路径是更改的,因此它会给您带来错误 您需要在此位置搜索外部SD卡
/存储/模拟/0您可以申请23+运行时权限,以便您可以引用此@HarshitTrivedi我已经这样做了,但我得到了相同的内容我使用此代码获取权限ActivityCompat.requestPermissions(Main\u Booster.this,新字符串[]{android.Manifest.permission.CLEAR_APP_CACHE,android.Manifest.permission.KILL_BACKGROUND_进程,android.Manifest.permission.WRITE_设置,android.Manifest.permission.BATTERY_统计,android.Manifest.permission.ACCESS_网络_状态,Manifest.permission.READ_外部_存储,android.Manifest.permission.WRITE_EXTERNAL_STORAGE},1);一旦通过进入应用程序设置手动检查,存储设置是否为ON,为什么不使用Environment.getExternalStoreage()?或者您需要SD卡和usb存储设备?