Search code examples
androidandroid-gallery

How to create application specific folder in android gallery?


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?

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.


Solution

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

    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.

    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.

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