Wednesday, April 20, 2016

Android using external Storage directory

The API getExternalFilesDir() creates files that are app specific. When the user uninstall the application, this directory and its contents will be deleted. Also the media scanner does not read from these directories. This directory can be used by the purposes where the content only belongs to user such as edited photos. Beginning with Android 4.4. it provides a method getExternalFilesDir that will give both internal and SD card directories that device declare as External directories. 

Now to save files that can also be accessed by other apps, we should use getExternalStoragePublicDirectory method. We can pass the type as either of DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES. The code is like this below 

public File getAlbumStorageDir(String albumName) {
    
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}

references

No comments:

Post a Comment