Android Instrumentation测试,创建目录 - 奇怪的行为

时间:2018-06-18 09:10:22

标签: java android android-context android-instrumentation

我正在尝试在我的应用的下载目录中创建一个文件,并注意到一些奇怪的事情。要获取外部文件目录,我需要像这样的上下文

public  File getExternalFileStorageDirectory(Context context){
    File file = new File(context.getExternalFilesDir(
            Environment.DIRECTORY_DOWNLOADS), 
     Constants.EXTERNAL_STORAGE_DIRECTORY_NAME);
    return file;
}
然后我尝试创建这样的目录:

File file = new File(getExternalFileStorageDirectory().getPath(),"100");"
file.mkdir();

关键是我传入getExternalFileStorageDirectory

的上下文类型

如果我通过这个背景

InstrumentationRegistry.getContext();目录未创建,我得到IOException,表示没有这样的目录。但是,如果我将此作为上下文传递, InstrumentationRegistry.getTargetContext();,然后创建目录。任何人都可以解释为什么会这样吗?

0 个答案:

没有答案
相关问题