在Android中,getExternalStorageDirectory()方法被废弃后,可以使用其他方法来获取SD卡路径位置。以下是一个解决方法的示例代码:
// 检查SD卡是否可用
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
// 获取所有外部存储设备列表
File[] externalStorageFiles = ContextCompat.getExternalFilesDirs(context, null);
// 循环遍历外部存储设备列表
for (File file : externalStorageFiles) {
if (file != null && !file.equals(context.getExternalFilesDir(null))) {
String sdCardPath = file.getAbsolutePath();
// 这里可以根据实际需求对sdCardPath进行处理
Log.d("SD Card Path", sdCardPath);
}
}
}
上述代码中,首先通过Environment.getExternalStorageState()方法检查SD卡是否可用。然后使用ContextCompat.getExternalFilesDirs()方法获取所有外部存储设备列表,返回的是一个File数组。接着遍历外部存储设备列表,通过getAbsolutePath()方法获取SD卡路径,并进行相应的处理。
请注意,这个方法返回的是外部存储设备的路径列表,可能包含多个路径,其中一个是主要的外部存储设备路径(通常是内置SD卡或存储)。你可以根据具体需求来选择正确的路径。