How to create file directories and folders in Android data/data/project filesystem
Asked Answered
O

5

16

I am working on a video editor program and am fairly new to android and java. What I would like to happen is when the user presses "create new project" button, a dialog pops up asking the user for the name of the project. I have that part down, but what I then want, is when the user presses "ok" on that dialog, my code will take the name and create a directory inside of my data/data file for my project and inside of that directory create folders titled 1 to 5 or more. I really don't know how to go about this, so any input would be truly appreciated.

Overline answered 11/4, 2011 at 20:45 Comment(0)
S
42

As sgarman proposed you can use the SD card (and I think it's better) but if you want to use your application dir you can get it by calling getFilesDir() from your activity, it will return a File object of /data/data/your.app/files then you can get the path and append the new directory:

String dirPath = getFilesDir().getAbsolutePath() + File.separator + "newfoldername";
File projDir = new File(dirPath);
if (!projDir.exists())
    projDir.mkdirs();
...
Scilla answered 11/4, 2011 at 21:34 Comment(2)
Thank you. What do you think about this one File projDir = new File(getFilesDir(), "newfoldername"); File projDir = new File(dirPath); if (!projDir.exists()) projDir.mkdirs(); Shouldn't this work?Redeploy
What if there is no SD card in the device?Shoestring
Q
6

The proper way to get a directory that, for the primary device owner, resides under Android/data/packagename on external storage, is just to call getExternalFilesDir() on an available Context.

That is,

File folder = context.getExternalFilesDir("YOUR FOLDER NAME");

And also you have to add write permission in Manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Questioning answered 26/7, 2016 at 11:39 Comment(0)
M
1

Check out the getFilesDir method which will return the directory you're looking for. You'll probably want to make a subdirectory as well.

Ml answered 11/4, 2011 at 21:30 Comment(0)
B
1

You must try this to create folder in Android/data/data/your.app/. use this in your onCreate method.

File file = this.getBaseContext().getExternalFilesDir("yourFolderName");
    if (!file.exists())
        file.mkdir();
Backstay answered 26/4, 2021 at 17:46 Comment(0)
C
0

This method work in my App. I use all method above discuss here not working

File file= new File(Environment.getExternalStorageDirectory().toString()+"/Android/data/com.package_name/your_folder_name")

this method used to create folder in data Directory of Your App This folder images and video not access in Gallery but manually access all files in your app use of folder path

Creosote answered 17/1, 2022 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.