如何创建上安装了Android应用程序在内部存储的目录? [英] How to create a directory on the internal storage upon installation of the Android application?

查看:197
本文介绍了如何创建上安装了Android应用程序在内部存储的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个单独的文件夹,我会把所有的资源,我会需要,我希望它的内部存储。因为我希望创建一次此目录中,我发现,我应该在应用程序的主要活动创建它:

I want to have a single folder where I will put all the resources I'm going to need and I want it on the internal storage. Since I want this directory to be created only once, I found out that I should create it in the main activity of the application:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    File directory = context.getDir("mydir", Context.MODE_PRIVATE);
    Log.d("directory", directory.getAbsolutePath().toString());
} 

这似乎确定,因为这是我能找到在互联网上,但我收到错误回报的负荷,我可以到日志的目录路径和应用程序不启动。知道为什么吗?

It seems ok as this is what I was able to find on the internet, however I am receiving loads of erros, I can get to the log for the directory path and the application doesn't start. Any idea why?

另外,如果我有内部存储每次工作时,我从Eclipse运行我的应用程序到我的手机,是否会自动与它的资源删除previous之一。另一个或我已经这样做手工?

Also, if I work with internal storage every time when I run my application from Eclipse to my phone, does it automatically deletes the previous one together with its resources or I've to do that manually?

推荐答案

使用 getCacheDir()。它返回的绝对路径文件系统上的应用程序特定的缓存目录。然后,您可以创建目录

use getCacheDir(). It returns the absolute path to the application specific cache directory on the filesystem. Then you can create your directory

File myDir = new File(getCacheDir(), "folder");
myDir.mkdir();

这篇关于如何创建上安装了Android应用程序在内部存储的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆