Thursday, September 15, 2016

Storage in Android and how to make sense of it

Storage in Android

Methods on Context 
Methods on Environment
  • Methods on Context 
    • Context.getFilesDir()       
    • Context.getCacheDir()
               These methods store the files in primary storage (internal SD card) which is protected
               This cannot be seen on an unrooted device when USB is connected.
               Files stored here are automatically removed when app is uninstalled.
               App doesn't require any specific permissions to read / write from this location.
    • Context.getExternalFilesDir()
    • Context.getExternalCacheDir()
               These methods store the files in primary storage (internal SD card) which is un-protected
               This can be seen on an unrooted device when USB is connected.
               Files stored here are automatically removed when app is uninstalled.
               App doesn't require any specific permissions to read / write from this location.

  • Methods on Environment
    • Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS ,FILENAME);
      • Environment.DIRECTORY_DOCUMENTS
      •  Environment.DIRECTORY_DOWNLOADS;
      •  Environment.DIRECTORY_MOVIES;
      •  Environment.DIRECTORY_MUSIC;
      •  Environment.DIRECTORY_NOTIFICATIONS;
      •  Environment.DIRECTORY_PICTURES;
      •  Environment.DIRECTORY_PODCASTS;
      •  Environment.DIRECTORY_RINGTONES; 

    • Environment.getExternalStorageDirectory()
                    Apps shouldn't really be using this location to store app specific data as it points to root 
                    of the storage unlike the specific directories like "Music / Pictures / Podcasts " as was the
                    case with previous call.

                    Files stored here are not automatically removed when app is uninstalled.

                    Writing to this location requires you to have WRITE_EXTERNAL_STORAGE and 
                    starting in kitkat reading requires READ_EXTERNAL_STORAGE which is
                    automatically granted if you have write permission
                     
                        











No comments:

Post a Comment