- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我到处寻找答案,每次看到其他人使用该方法时:
getFilesDir();
但是当我尝试以任何方式使用该方法时,尤其是:
File myFile = new File (getFilesDir();, filename );
Eclipse 只是说,“无法从类型 ContextWrapper 对非静态方法 getFilesDir 进行静态引用”
我正在尝试使用它来获取内部目录来为我的应用程序编写文件。
谢谢!
最佳答案
那是因为在静态方法中你没有得到类的对象,getFilesDir 不是静态方法,这意味着它只能通过类 Context 的对象访问。
所以您可以做的是将对象的引用存储在类的静态变量中,然后在静态方法中使用它。
例如:
static YourContextClass obj;
static void method(){
File myFile = new File (obj.getFilesDir(), filename );
}
您还必须在 onCreateMethod() 中存储对对象的引用
obj = this;
最好的办法是
static void method(YourContextClass obj){
File myFile = new File (obj.getFilesDir(), filename );
}
关于android - 为什么我不能使用 getFilesDir();在静态环境中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15784616/
我可以在使用 extends View 的 .class 中使用方法 getFilesDir() 吗? public void save(){ try { File myDir
我需要将文件下载到设备的内部存储器中。我这样做: Uri downloadUri = Uri.parse(url); DownloadManager.Request request = new Dow
我正在使用以下代码从网络和 APK 更新文件下载: URL url = new URL(urls[0]); HttpURLConnection connection2
我正处于学习 Java 的早期阶段,但希望有人能解释我在这里做错了什么,大量的 Google 搜索让我头晕目眩。 我一直在学习如何将 ArrayList 保存到文本文件然后获取详细信息的教程。 我收到
我正在尝试在应用程序第一次运行时为每个设备生成并保存一个 UUID。我通过 getFilesDir() 将这个 UUID 保存到一个文件中,但问题是如果用户通过 android 设置删除我的应用程序缓
我正在通过 SQLCipher 实现对 SQLite 数据库的访问在我的 Android 混合 Cordova 应用程序中,它使用一个自定义插件(即由我编写)。 SQLCipher 文档以及其他有关在
这个问题已经有答案了: 奥 git _a (12 个回答) 奥 git _a (1 个回答) 已关闭 5 年前。 我在 getfilesdir() 上收到 NULL 指针消息。我确信这与我的实现有关,
作为 I/O 的新手,我已经阅读并尝试使用 this 中的示例。和 this 。但是,我无法在内部存储中创建简单的目录。肯定是某个地方写错了。 对于我的应用程序,我使用以下 file_paths.xm
我到处寻找答案,每次看到其他人使用该方法时: getFilesDir(); 但是当我尝试以任何方式使用该方法时,尤其是: File myFile = new File (getFilesDir();,
我正在 Android 中做一个简单的应用程序,在应用程序的某个部分我想创建一个 Excel 文件并写入其中。我已经准备好使用 jexcel 库使用 Java 编辑 excel 的所有内容,但问题是我
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
在第一次启动应用程序时,我使用 context.getFilesDir() 创建一个文件来获取存储路径。这工作正常,除了极少数情况外,此方法返回“/”根文件夹。结果,应用程序崩溃并显示: java.i
我是 Android 编程的新手,所以我的问题的答案希望很简单。 我有一个应用程序可以从通过网络连接的电脑程序接收文件。该文件然后在内部存储并由应用程序使用。当应用程序启动时,它会读取文件(它是类“W
调用 getFilesDir() 时崩溃,这是代码 public void generateFile() throws IOException{ File fileDir = getFiles
我想访问 getFilesDir() 这是一个上下文方法。 有什么方法可以像访问外部存储一样访问它吗? Environment.getExternalStorageDirectory(); 作为环境变
我正在使用 Android Studio,我想知道应用程序的目录(指每个非静态类)。 问题是,我正在创建一个名为 Constants 的类来总结应用程序中的任意常量。除了与应用程序目录相关的常量之外,
据我所知,使用 getFilesDir() 来存储数据(在我的例子中是相机捕获的数据)有点临时。我这么说是因为如果我关闭并重新打开应用程序,我可以访问目录和文件列表,但不能访问文件本身。 我的逻辑是错
这是我正在做的: 1- 从我的 Assets 中获取文件并将其放入字符串中 - 成功 2- 在 getFilesDir() 中创建一个文件 - 失败(还没有崩溃) 3- 使用 FileWriter 将
这希望没有被问过很多次(或根本没有!),但我想知道如何 Context当通过它的 打开应用程序时,s 将起作用来自另一个应用程序,而不是它的 MAIN LAUNCHER Intent 。 本质上,所
返回文件对象的“getDataDir()”已从 API 24 添加。如果使用它,它要求最低 API 级别为 24。 文档将其用法解释为: Returns the absolute path to th
我是一名优秀的程序员,十分优秀!