How to create a directory on the internal storage

2019-03-15 19:47发布

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?

6条回答
欢心
2楼-- · 2019-03-15 20:12
<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
    }
查看更多
Evening l夕情丶
3楼-- · 2019-03-15 20:16

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();
查看更多
Anthone
4楼-- · 2019-03-15 20:25

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);
    }
查看更多
干净又极端
5楼-- · 2019-03-15 20:28

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

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

In your AndroidManifest.xml file

Also this for working with the storage:

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

if(!direct.exists()) {
     if(direct.mkdir()); //directory is created;
}
查看更多
不美不萌又怎样
6楼-- · 2019-03-15 20:29

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

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:

"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.

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.

查看更多
We Are One
7楼-- · 2019-03-15 20:32

Android studio Download video in specific folder:

  <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();
查看更多
登录 后发表回答