【问题标题】:How Can i change The Default Location Of My Working Directory?如何更改我的工作目录的默认位置?
【发布时间】:2016-05-16 16:41:52
【问题描述】:

我正在使用以下方法来设置我的工作目录

 File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "xxxxxx");

现在的问题是,如果手机有多个 SD 卡,这给了我可变的位置。有时,它会自动将外部 Sd 卡作为默认位置,但在三星等某些手机中,它总是将内部 Sd 卡作为默认位置,即 storage/sdcard0/Pictures/xxxxxxx。 我想要的是存储/extsdcard/图片/xxxxxxx。但我不想硬编码路径。而且,我听说在 Kitkat 版本中,无法将文件放入外部 Sdcard。那么我该如何处理这两个问题呢?

注意:我尝试将三星的默认存储位置更改为外部 SdCard,但没有成功。

【问题讨论】:

    标签: android memory local-storage


    【解决方案1】:

    根据您的 API 要求,您是否尝试过使用此功能:

    API 级别 8+

    Context.getExternalFilesDirs()
    

    API 级别 7-

    Context.getExternalStorageDirectory();
    

    问题可能是您在设备(或模拟器)上使用 API 8 或更高版本运行它,那么您需要使用第一种方法。如果您的应用应该支持低于 8 的 API,那么我建议您使用 if then 语句来检查实际的 API 版本,如下所示:

    if (Build.VERSION.SDK_INT >= 8) {
        Context.getExternalFilesDirs()
    }else{
        Context.getExternalStorageDirectory();
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多