Search code examples
androidandroid-cameraandroid-internal-storage

Accessing internal storage in Android


I'm developing an Android app that needs to display images from the default camera folder. My problem is that I do not know how to find what is the internal default storage folder for camera photos.

When I look for it connecting the phone to the PC (it is a Samsung Galaxy S7 without SD card), I see that the pictures are stored in DCIM/Camera. However, this does not work. Doing: files = new File("DCIM/Camera/"); results in the app saying there is nothing there.

How can I find the default internal storage folder for camera photos?


Solution

  • Firstly, you need to understand Android.

    • Internal storage is the app private storage
    • External storage is the publically accessible storage area

    External storage in Android means the public storage and means internal storage as in device memory and SD card. Saving to external storage means device internal and SD card, while saving to itnernal storage means app private storage.

    So, the images are stored in the external storage or the app public storage where any app can save, load and manage files. The images are publically available and thus not in internal storage by developer definition.

    Now, you need the WRITE_EXTERNAL_STORAGE permission and if you target API 23 you also need to request it because it is a dangerous permission.

    To access the images, you need to create a new file:

    storageDir = new File(
        Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES
        ), 
        "album name here, or remove this part to save or load directly into DCIM"
    );
    

    This can be used to save or load images, but remember to index the directory to find the image before loading to prevent IOException