eclipse “getApplicationContext().getFilesDir()”返回什么路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28775949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What path does "getApplicationContext().getFilesDir()" return?
提问by adriapm
I'm doing a simple app in Android and in a certain part of the app I would like to create an Excel file and write in it. I've already prepared everything to use jexcel library to edit an excel using Java, but the thing is I can't find the Excel file I created. I've tried to find it in my own device executing the app, but I couldn't.
我正在 Android 中做一个简单的应用程序,在应用程序的某个部分我想创建一个 Excel 文件并在其中写入。我已经准备好使用 jexcel 库来使用 Java 编辑 excel 的所有内容,但问题是我找不到我创建的 Excel 文件。我试图在我自己的设备中找到它执行应用程序,但我不能。
String fileName = "hours.xls";
File file = new File(getApplicationContext().getFilesDir() + fileName);
Can anybody help me please?
有人可以帮我吗?
Thanks in advance :)
提前致谢 :)
回答by bmat
On Android KitKat, it returns /data/data/{your package name}/files
, however I imagine this could change depending on your platform version. Thus if you're just trying to dig through your filesystem and see a file, it's safe to use this path, but if you're using this path for some functionality across multiple platform versions, you should only reference it using getFilesDir()
.
在 Android KitKat 上,它返回/data/data/{your package name}/files
,但是我想这可能会根据您的平台版本而改变。因此,如果您只是想在文件系统中挖掘并查看文件,则使用此路径是安全的,但如果您将此路径用于跨多个平台版本的某些功能,则应仅使用getFilesDir()
.
回答by Shawn Hazen
What are you planning on using this file for? Do you want it usable by other apps too? Using getApplicationContext().getFilesDir()
will give you /data/data/com.package/files
but if you want a file that's easily accessible by yourself and other apps, you're better off using something like getExternalFilesDir()
你打算用这个文件做什么?你想让它也被其他应用程序使用吗?使用getApplicationContext().getFilesDir()
会给你,/data/data/com.package/files
但如果你想要一个你自己和其他应用程序可以轻松访问的文件,你最好使用类似的东西getExternalFilesDir()
回答by coutier eric
If you want to access your file via your PC (with an usb cable) or via a file manager on your device, prefer:
如果您想通过您的 PC(使用 USB 电缆)或通过您设备上的文件管理器访问您的文件,请选择:
new File(getExternalFilesDir(null), fileName);
This folder is created in .../Android/data/ ... com.yoursociety.yourapp/files ...
该文件夹创建于 .../Android/data/ ... com.yoursociety.yourapp/files ...
null means that you do not want to store files in predefined folders like Movies, Pictures and so on.
null 表示您不想将文件存储在电影、图片等预定义文件夹中。
(See documentationfor more info)
(有关更多信息,请参阅文档)
回答by Soul Panda
This worked:
这有效:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
b = (Brain)load("brain.txt");
if (b == null) {
b = new Brain();
}
vocabulary = (ArrayList <String>) load("vocabulary.txt");
if (vocabulary == null) {
vocabulary = new ArrayList <String> ();
vocabulary.add("I love you.");
vocabulary.add("Hi!");
}
b.setRunning(true);
}
public Object load(String fileName) {
File file = new File("/storage/emulated/0/Android/data/com.cobalttechnology.myfirstapplication/files/" + fileName);
if (!file.exists()) {
return null;
}
try {
Object o;
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
o = ois.readObject();
if (o == null) {
System.out.println(fileName + " = null");
}
ois.close();
fis.close();
System.out.println("Loaded: " + fileName);
return o;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return null;
}
public void save(Object o, String fileName) {
File file = new File("/storage/emulated/0/Android/data/com.cobalttechnology.myfirstapplication/files/" + fileName);
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(o);
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
回答by Jorgesys
Read the documentation, this method reads the files stored in the internal storage that were created with
with openFileOutput()
:
阅读文档,此方法读取存储在内部存储器中的文件,这些文件是使用以下方法创建的openFileOutput()
:
getFilesDir()
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.
获取文件目录()
返回文件系统上存储使用 openFileOutput(String, int) 创建的文件的目录的绝对路径。