为应用程序创建一个文件夹(Create a file folder for app)

下面的代码不会在我的设备中创建一个文件夹。

String intStorageDirectory = context.getFilesDir().toString(); File folder = new File(intStorageDirectory, "test"); folder.createNewFile();;

我需要为我的应用程序创建的文件夹来存储媒体,当用户安装它。 该文件夹应该在文件资源管理器中可见。 我该怎么做?

The below code doesn't create a folder in my device.

String intStorageDirectory = context.getFilesDir().toString(); File folder = new File(intStorageDirectory, "test"); folder.createNewFile();;

I need a folder created for my app to store media, when user installs it. That folder should be visible on file explorer. How can i do it?

最满意答案

使用您创建文件的当前片段,您还可以通过创建文件来创建文件夹,但当前目录是基本文件夹,getFilesDir()指向您的应用程序的内部存储 ,除非明确声明,否则该内部存储不可见也不可访问。 您可以通过使用new File().createNewFile()创建文件夹和文件,或使用mkdirs()创建仅文件夹,但您将无法使用文件资源管理器应用程序显示它,并且文件夹和文件中的文件将是删除/如果用户卸载您的应用程序。

要外部保存文件(这并不意味着保存到SD卡),您可以使用创建目录和文件

File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), folderName); if (!mediaStorageDir.exists()) { mediaStorageDir.mkdirs() } File mediaFile = new File(mediaStorageDir.getAbsolutePath() + File.separator + fileName);

而且您需要某种OutputStream来将数据写入该文件。

确保你在你的AndroidManifest.xml文件中询问<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />并在运行时询问写入权限,如果你的android:targetSdkVersion="23"或以上

With the current snippet you created a file, you can also create folder by creating file but your current directory is the base folder, getFilesDir() points internal storage for your app which not visible nor accessible unless explicitly declared. You can create a folder and file by creating with new File().createNewFile() or create only folder using mkdirs() but you won't be able to display it using a file explorer app and that folder and files inside it will be deleted when/if user uninstalls your app.

To save files externally(This doesn't mean saving to SD Card) you can create directory and file with

File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), folderName); if (!mediaStorageDir.exists()) { mediaStorageDir.mkdirs() } File mediaFile = new File(mediaStorageDir.getAbsolutePath() + File.separator + fileName);

And you need some kind of OutputStream to write data to that file.

Make sure that you ask <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> inside your AndroidManifest.xml file and ask write permission on runtime if your android:targetSdkVersion="23" or above

为应用程序创建一个文件夹(Create a file folder for app)

下面的代码不会在我的设备中创建一个文件夹。

String intStorageDirectory = context.getFilesDir().toString(); File folder = new File(intStorageDirectory, "test"); folder.createNewFile();;

我需要为我的应用程序创建的文件夹来存储媒体,当用户安装它。 该文件夹应该在文件资源管理器中可见。 我该怎么做?

The below code doesn't create a folder in my device.

String intStorageDirectory = context.getFilesDir().toString(); File folder = new File(intStorageDirectory, "test"); folder.createNewFile();;

I need a folder created for my app to store media, when user installs it. That folder should be visible on file explorer. How can i do it?

最满意答案

使用您创建文件的当前片段,您还可以通过创建文件来创建文件夹,但当前目录是基本文件夹,getFilesDir()指向您的应用程序的内部存储 ,除非明确声明,否则该内部存储不可见也不可访问。 您可以通过使用new File().createNewFile()创建文件夹和文件,或使用mkdirs()创建仅文件夹,但您将无法使用文件资源管理器应用程序显示它,并且文件夹和文件中的文件将是删除/如果用户卸载您的应用程序。

要外部保存文件(这并不意味着保存到SD卡),您可以使用创建目录和文件

File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), folderName); if (!mediaStorageDir.exists()) { mediaStorageDir.mkdirs() } File mediaFile = new File(mediaStorageDir.getAbsolutePath() + File.separator + fileName);

而且您需要某种OutputStream来将数据写入该文件。

确保你在你的AndroidManifest.xml文件中询问<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />并在运行时询问写入权限,如果你的android:targetSdkVersion="23"或以上

With the current snippet you created a file, you can also create folder by creating file but your current directory is the base folder, getFilesDir() points internal storage for your app which not visible nor accessible unless explicitly declared. You can create a folder and file by creating with new File().createNewFile() or create only folder using mkdirs() but you won't be able to display it using a file explorer app and that folder and files inside it will be deleted when/if user uninstalls your app.

To save files externally(This doesn't mean saving to SD Card) you can create directory and file with

File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), folderName); if (!mediaStorageDir.exists()) { mediaStorageDir.mkdirs() } File mediaFile = new File(mediaStorageDir.getAbsolutePath() + File.separator + fileName);

And you need some kind of OutputStream to write data to that file.

Make sure that you ask <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> inside your AndroidManifest.xml file and ask write permission on runtime if your android:targetSdkVersion="23" or above