如何在安装Android应用程序时在内部存储上创建目录?

时间:2022-09-30 07:11:55

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?

看起来还好,因为这是我在internet上可以找到的,但是我正在接收大量的erros,我可以访问目录路径的日志,而应用程序没有启动。知道为什么吗?

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?

而且,如果我每次运行我的应用程序从Eclipse到我的手机时都使用内部存储,它是否会自动删除之前的一个和它的资源,或者我必须手动地删除它?

5 个解决方案

#1


15  

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

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

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

#2


5  

First of all, make sure to read up on and understand the different storage options you have available for your application. Google has a very good article on the subject here:

首先,确保阅读并理解应用程序中可用的不同存储选项。谷歌在这方面有一篇很好的文章:

http://developer.android.com/guide/topics/data/data-storage.html

http://developer.android.com/guide/topics/data/data-storage.html

The answer above from Andy mentions Environment.getExternalStorageDirectory() (API Level 7) for writing to an SD card or similar, but you should keep in mind that files written to that location will not get deleted when the user uninstalls the app. The documentation says:

上面的答案是Andy提到的环境. getexternalstoragedirectory () (API级别7),用于写入SD卡或类似的文件,但您应该记住,当用户卸载应用程序时,写入该位置的文件不会被删除。

"Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace"

“应用程序不应该直接使用这个*目录,以避免污染用户的根名称空间”

Instead, if you target API Level 8 and above, you should use Context.getExternalCacheDir() as files in that location will get removed when the app is uninstalled. If you really want files to persist even after an uninstall you should use Context.getExternalFilesDir(). There's also getCacheDir() and getFilesDir() for working with files on the internal storage.

相反,如果目标是API级别8或更高的级别,则应该使用Context.getExternalCacheDir(),因为当卸载应用程序时,该位置的文件将被删除。如果您真的想要文件在卸载后仍然保存,您应该使用Context.getExternalFilesDir()。还有getCacheDir()和getFilesDir()用于处理内部存储上的文件。

As was mentioned in the comments to your question you really should post the errors here for us to be able to help you.

正如在你的问题的评论中提到的,你真的应该在这里张贴错误,以便我们能够帮助你。

PS I'm new to answering questions on SO so I don't have enough rep to post links to all of the functions mentioned in my answer and I do not seem able to comment on previous posts.

PS:我对回答问题很陌生,所以我没有足够的代表来发布我回答中提到的所有功能的链接,而且我似乎也不能对以前的帖子发表评论。

#3


5  

IF you would like to store on the external storage SD card instead, you need to have this

如果您想要存储在外部存储SD卡上,您需要有这个。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

In your AndroidManifest.xml file

在你的AndroidManifest。xml文件

Also this for working with the storage:

这也适用于存储工作:

File direct = new File(Environment.getExternalStorageDirectory()+"/folder");

if(!direct.exists()) {
     if(direct.mkdir()); //directory is created;
}

#4


0  

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

File newfile = new File(Environment.getExternalStorageDirectory()+"/specifyfoldername","nestedfoldername");
    direct.mkdir();
// it will create two folder in your internal storage first is specifyfoldername and another one inside the mentioned folder which is nestedfoldername
    }

#5


0  

I think Blackbelt answer is good but did not want to store it on internal cache (it will get erased and written to often that way for the specs I was trying to make) so what i did is:

我认为黑带回答是好的,但不想将它存储在内部缓存(它将被删除,并经常以这种方式写入我正在尝试的规格),所以我所做的是:

String path = "";


And then

然后

if (path.isEmpty()) {
        File file = new File(this.getFilesDir(), "my_dir_identfiying_name/");
        path = file.getAbsolutePath();
        Log.d("PATH", path);
    }

#1


15  

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

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

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

#2


5  

First of all, make sure to read up on and understand the different storage options you have available for your application. Google has a very good article on the subject here:

首先,确保阅读并理解应用程序中可用的不同存储选项。谷歌在这方面有一篇很好的文章:

http://developer.android.com/guide/topics/data/data-storage.html

http://developer.android.com/guide/topics/data/data-storage.html

The answer above from Andy mentions Environment.getExternalStorageDirectory() (API Level 7) for writing to an SD card or similar, but you should keep in mind that files written to that location will not get deleted when the user uninstalls the app. The documentation says:

上面的答案是Andy提到的环境. getexternalstoragedirectory () (API级别7),用于写入SD卡或类似的文件,但您应该记住,当用户卸载应用程序时,写入该位置的文件不会被删除。

"Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace"

“应用程序不应该直接使用这个*目录,以避免污染用户的根名称空间”

Instead, if you target API Level 8 and above, you should use Context.getExternalCacheDir() as files in that location will get removed when the app is uninstalled. If you really want files to persist even after an uninstall you should use Context.getExternalFilesDir(). There's also getCacheDir() and getFilesDir() for working with files on the internal storage.

相反,如果目标是API级别8或更高的级别,则应该使用Context.getExternalCacheDir(),因为当卸载应用程序时,该位置的文件将被删除。如果您真的想要文件在卸载后仍然保存,您应该使用Context.getExternalFilesDir()。还有getCacheDir()和getFilesDir()用于处理内部存储上的文件。

As was mentioned in the comments to your question you really should post the errors here for us to be able to help you.

正如在你的问题的评论中提到的,你真的应该在这里张贴错误,以便我们能够帮助你。

PS I'm new to answering questions on SO so I don't have enough rep to post links to all of the functions mentioned in my answer and I do not seem able to comment on previous posts.

PS:我对回答问题很陌生,所以我没有足够的代表来发布我回答中提到的所有功能的链接,而且我似乎也不能对以前的帖子发表评论。

#3


5  

IF you would like to store on the external storage SD card instead, you need to have this

如果您想要存储在外部存储SD卡上,您需要有这个。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

In your AndroidManifest.xml file

在你的AndroidManifest。xml文件

Also this for working with the storage:

这也适用于存储工作:

File direct = new File(Environment.getExternalStorageDirectory()+"/folder");

if(!direct.exists()) {
     if(direct.mkdir()); //directory is created;
}

#4


0  

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

File newfile = new File(Environment.getExternalStorageDirectory()+"/specifyfoldername","nestedfoldername");
    direct.mkdir();
// it will create two folder in your internal storage first is specifyfoldername and another one inside the mentioned folder which is nestedfoldername
    }

#5


0  

I think Blackbelt answer is good but did not want to store it on internal cache (it will get erased and written to often that way for the specs I was trying to make) so what i did is:

我认为黑带回答是好的,但不想将它存储在内部缓存(它将被删除,并经常以这种方式写入我正在尝试的规格),所以我所做的是:

String path = "";


And then

然后

if (path.isEmpty()) {
        File file = new File(this.getFilesDir(), "my_dir_identfiying_name/");
        path = file.getAbsolutePath();
        Log.d("PATH", path);
    }