简体   繁体   English

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

[英]How to create a directory on the internal storage upon installation of the Android application?

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? 知道为什么吗?

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运行应用程序到手机时都使用内部存储器,它是否会自动删除前一个应用程序及其资源,或者我是手动执行此操作?

use getCacheDir() . 使用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();

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;
}

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. Andy上面的答案提到了Environment.getExternalStorageDirectory()(API级别7)用于写入SD卡或类似卡,但是您应该记住,当用户卸载应用程序时,写入该位置的文件不会被删除。 The documentation says: 文件说:

"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. 相反,如果您将API级别8及更高版本作为目标,则应该使用Context.getExternalCacheDir()作为该位置中的文件,这将在卸载应用程序时被删除。 If you really want files to persist even after an uninstall you should use Context.getExternalFilesDir(). 如果您确实希望文件在卸载后仍然存在,则应使用Context.getExternalFilesDir()。 There's also getCacheDir() and getFilesDir() for working with files on the internal storage. 还有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我是新来回答有关SO的问题,所以我没有足够的代表发布我的答案中提到的所有功能的链接,我似乎无法评论以前的帖子。

<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
    }

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: 我认为Blackbelt的答案很好,但不想将它存储在内部缓存中(它会被删除并写入我经常尝试制作的规格)所以我所做的是:

String path = "";


And then 然后

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

Android studio Download video in specific folder: Android studio在特定文件夹中下载视频:

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

  String path = Environment.getDataDirectory().getAbsolutePath().toString() + "/storage/emulated/0/appFolder";
  File mFolder = new File(path);
  if (!mFolder.exists()) {
      mFolder.mkdir();
  }
  File Directory = new File("/sdcard/kaizzan/");
  Directory.mkdirs();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM