如何在Android data/data/project文件系统中创建文件目录和文件夹

发布于 2024-10-31 19:18:36 字数 211 浏览 10 评论 0原文

我正在开发一个视频编辑器程序,并且对 android 和 java 相当陌生。我希望发生的是,当用户按下“创建新项目”按钮时,会弹出一个对话框,询问用户项目的名称。我已经把那部分写下来了,但是我想要的是,当用户在该对话框上按“确定”时,我的代码将采用该名称并在我的项目的数据/数据文件内创建一个目录,并在该目录内创建一个目录标题为 1 到 5 或更多的文件夹。我真的不知道该怎么做,所以任何意见都会非常感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终陌 2024-11-07 19:18:36

正如 sgarman 提议的那样,您可以使用 SD 卡(我认为它更好),但如果您想使用应用程序目录,您可以通过从 Activity 中调用 getFilesDir() 来获取它,它将返回 < /data/data/your.app/files 的 code>File 对象,然后您可以获得路径并附加新目录:

String dirPath = getFilesDir().getAbsolutePath() + File.separator + "newfoldername";
File projDir = new File(dirPath);
if (!projDir.exists())
    projDir.mkdirs();
...

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();
...
久隐师 2024-11-07 19:18:36

对于主要设备所有者来说,获取驻留在外部存储上的 Android/data/packagename 下的目录的正确方法是在可用的上下文上调用 getExternalFilesDir() 。

也就是说,

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

你还必须在 Manifest 中添加写权限

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

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"/>
︶葆Ⅱㄣ 2024-11-07 19:18:36

查看 getFilesDir 方法,该方法将返回您正在寻找的目录。您可能还想创建一个子目录。

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

德意的啸 2024-11-07 19:18:36

您必须尝试在 Android/data/data/your.app/ 中创建文件夹。
在您的 onCreate 方法中使用它。

File file = this.getBaseContext().getExternalFilesDir("yourFolderName");
    if (!file.exists())
        file.mkdir();

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();
素食主义者 2024-11-07 19:18:36

这个方法在我的应用程序中有效。我使用上面讨论的所有方法都不起作用

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

此方法用于在应用程序的数据目录中创建文件夹此文件夹图像和视频无法在图库中访问,但可以使用文件夹路径手动访问应用程序中的所有文件

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文