获取getExternalFilesDir()的子路径的最佳方法是什么?

时间:2014-11-11 16:13:33

标签: android

getExternalFilesDir(type)返回主要"外部"的路径存储。我需要我的应用程序将文件存储在真正的外部存储(即SD卡)中,但我仍然希望使用正确的目录结构来获取应用数据。获取子路径相对于存储目录的最佳方法是什么?

我目前正在这样做:

// for type = Environment.DIRECTORY_MUSIC
// extPath = "/storage/emulated/0/Android/data/com.example.app/files/Music"
String extPath = context.getExternalFilesDir(type).toString();
int extStoragePathLen = Environment.getExternalStorageDirectory().toString().length();
// subPath = "/Android/data/com.example.app/files/Music"
String subPath = extPath.substring(extStoragePathLen, extPath.length());
File sdCardPath = new File(getRealExternalStorageDirectory(), subPath);

注意:我没有使用Context.getExternalFilesDirs(type),因为它仅适用于SDK> = KitKat,因此可能在接下来的5年或6年内无法使用,直到KitKat成为当天真正的旧版Android

0 个答案:

没有答案