如何在android库中创建特定于应用程序的文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20523658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:58:52  来源:igfitidea点击:

How to create application specific folder in android gallery?

androidandroid-gallery

提问by Harshal Kshatriya

I'm building android application "XYZ" in which users will be storing media such as images and videos. Therefore, I would like to create a folder in android gallery named "XYZ" and store all the media specific to the application in "XYZ". How do I achieve it?

我正在构建 android 应用程序“ XYZ”,用户将在其中存储图像和视频等媒体。因此,我想在 android 库中创建一个名为 "XYZ" 的文件夹,并将所有特定于该应用程序的媒体存储在 "XYZ" 中。我如何实现它?

Currently, I'm able to store the media on sdcard and on scanning the media files, they show up in "images" folder, in android gallery.

目前,我可以将媒体存储在 sdcard 上,并在扫描媒体文件时,它们会显示在 android 库中的“图像”文件夹中。

回答by Yaroslav Mytkalyk

I guess the proper way of storing your images accessible by galleries is writing the files to this directory.

我想存储画廊可以访问的图像的正确方法是将文件写入此目录。

static final String appDirectoryName = "XYZ";
static final File imageRoot = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES), appDirectoryName);

And for videos

对于视频

static final File videoRoot = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_MOVIES), appDirectoryName);

And to create an image

并创建图像

imageRoot.mkdirs();
final File image = new File(imageRoot, "image1.jpg");
// open OutputStream and write the file

If you add some files and open the gallery you should see album "XYZ". Note that you will not see the album if the directory is empty.

如果您添加一些文件并打开图库,您应该会看到专辑“XYZ”。请注意,如果目录为空,您将看不到相册。

And about your words

还有你的话

Currently, I'm able to store the media on sdcard and on scanning the media files, they show up in "images" folder, in android gallery.

目前,我可以将媒体存储在 sdcard 上,并在扫描媒体文件时,它们会显示在 android 库中的“图像”文件夹中。

The representation depends on the Gallery app you use. The default Android gallery should create "Albums" named after the folder the media files are in.

表示方式取决于您使用的图库应用程序。默认的 Android 画廊应该创建以媒体文件所在文件夹命名的“相册”。

Edit: you will not see newly created image if you don't run media scanner or add it to MediaStore database.

编辑:如果您不运行媒体扫描仪或将其添加到 MediaStore 数据库,您将看不到新创建的图像。

回答by CommonsWare

There are > 1 billion Android devices, representing thousands of device models, with hundreds of different "gallery" apps (pre-installed or user-installed). There is no requirement for any "gallery" app to have the concept of folders, let alone for third-party developers to control those folders.

有超过 10 亿台 Android 设备,代表数千种设备型号,拥有数百种不同的“图库”应用程序(预安装或用户安装)。没有要求任何“图库”应用程序具有文件夹的概念,更不用说第三方开发人员来控制这些文件夹了。

Hence, there is no general way of achieving your objective, other than to write your own such app.

因此,除了编写自己的此类应用程序外,没有其他通用方法可以实现您的目标。

回答by Matan Dahan

If you are able to store the videos and images in external storage like SD card, I assume you know how to use "FILES"

如果您能够将视频和图像存储在 SD 卡等外部存储器中,我假设您知道如何使用“文件”

therefore you just need to name the location you want to save in and create it (if it doesnt exist)

因此,您只需要命名要保存的位置并创建它(如果它不存在)

something like:

就像是:

File file = new File(Environment.getExternalStorageDirectory() +"/XYZ/");  

if(!file.exists())     //check if file already exists
{
    file.mkdirs();     //if not, create it
}

this is your Directory Folder, and you can save in it what ever you want.

这是您的目录文件夹,您可以在其中保存任何您想要的内容。

something like:

就像是:

File imageFile = new File(file.toString() + "image" + ".jpg");

This will create an image file called "image.jpg" in the directory XYZ.

这将在目录 XYZ 中创建一个名为“image.jpg”的图像文件。