Skip to content

Reading and writing files on external storage

rutura edited this page Apr 17, 2017 · 1 revision
  • You get a path to the external storage and create a folder there like this:
          File rootPath = new File(Environment.getExternalStorageDirectory(), "directory_name");
        if (!rootPath.exists()) {
            if(rootPath.mkdirs())
            {
                Log.d("ANDROID_TEST","Directory create success :"+ rootPath.getAbsolutePath());
            }else
            {
                Log.d("ANDROID_TEST","FAILED TO CREATE DIRECTORY :"+ rootPath.getAbsolutePath());
            }
        }
  • You then create files and work with them as you do in standard Java I/O like this:
           File dataFile = new File(rootPath, FILENAME);
  • Notable external storage directory APIs you should care about:

    • Environment.getExternalStoragePublicDirectory(String type)
      • API Level 8
      • Returns a common directory where all applications store media files. The contents of these directories are visible to users and other applications. In particular, the media placed here will likely be scanned and inserted into the device’s MediaStore for applications such as the Gallery.
      • Valid type values include DIRECTORY_PICTURES, DIRECTORY_MUSIC, DIRECTORY_MOVIES, and DIRECTORY_RINGTONES.
    • Context.getExternalFilesDir(String type)
      • API Level 8
      • Returns a directory on external storage for media files that are specific to the application. Media placed here will not be considered public, however, and won’t show up in MediaStore.
      • This is external storage, however, so it is still possible for users and other applications to see and edit the files directly: there is no security enforced.
      • Files placed here will be removed when the application is uninstalled, so it can be a good location in which to place large content files the application needs that one may not want on internal storage.
      • Valid type values include DIRECTORY_PICTURES, DIRECTORY_MUSIC, DIRECTORY_ MOVIES, and DIRECTORY_RINGTONES.
    • Context.getExternalCacheDir()
      • API Level 8
      • Returns a directory on internal storage for app-specific temporary files. The contents of this directory are visible to users and other applications.
      • Files placed here will be removed when the application is uninstalled, so it can be a good location in which to place large content files the application needs that one may not want on internal storage.
  • Also remember proper permissions for external storage:

            <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Clone this wiki locally