我在 android 2.2 上构建了一个 android 应用程序,为了将文件保存到 SD 卡中,我使用以下方法:
context.getExternalFilesDir(null).getAbsolutePath();
返回如下字符串:
/mnt/sdcard/Android/data/com.hello.example1/files
现在我需要让我的应用程序兼容android 2.1,我需要使用什么方法来获取外部文件目录?
public static String sTellMeWhereToSaveMyData(Context context)
{
String packageName = context.getPackageName();
File externalPath = Environment.getExternalStorageDirectory();
File appFiles = new File(externalPath.getAbsolutePath() + "/Android/data/" + packageName+ "/");
if (appFiles.exists() && appFiles.isDirectory())
{
return appFiles.getAbsolutePath();
}
else
{
if(appFiles.exists())
{
Log.v("File Manager","not exists");
}
if (!appFiles.mkdir())
{
Log.v("File Manager","Could not create");
}
}
return appFiles.getAbsolutePath();
}
最佳答案
你应该自己编写路径:
String packageName = context.getPackageName();
File externalPath = Environment.getExternalStorageDirectory();
File appFiles = new File(externalPath.getAbsolutePath() +
"/Android/data/" + packageName + "/files");
关于java - android 2.1 中的 getExternalFilesDir 替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6263641/