Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to store data in external files in android ?

    • 0
    • 8
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 227
    Comment on it

    In android data can be stored in external storage, where apps can save files. And these files can be accessed by other applications or user can extract them by connecting the device to a computer via USB.

    By using getExternalStorageState() we can get the current state of the primary external storage device. If its equal to Environment.MEDIA_MOUNTED then well have read/write access and if equal to Environment.MEDIA_MOUNTED_READ_ONLY then we have only read access.

    Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)

    Before we discuss these two types note that in order to read and/or write files to the external storage well need the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE system permissions.

    There are 2 methods that we can use to get the public external storage directory for placing files:

    - getExternalStorageDirectory() This returns the primary (top-level or root) external storage directory.

    - getExternalStoragePublicDirectorty() This returns a top level public external storage directory for shoving files of a particular type based on the argument passed. So basically the external storage has directories like Music, Podcasts, Pictures, etc. whose paths can be determined and returned via this function by passing the appropriate environment constants.

    String content = "Data To be Stored";
    File file;
    FileOutputStream outputStream;
    try {
        file = new File(Environment.getExternalStorageDirectory(), "MyFile");
    
        outputStream = new FileOutputStream(file);
        outputStream.write(content.getBytes());
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    As simple as that! You can try Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) instead of Environment.getExternalStorageDirectory() to save the file in the Downloads directory of your external storage.

    This SO answer has some extra details regarding these methods worth reading and knowing.

    Public files

    Files thatll remain on the storage even after the application is uninstalled by the user like media (photos, videos, etc.) or other downloaded files.

    Private files

    These should be files belonging to your app thatll get deleted once the user uninstalls the app. These files will be accessible to other apps but are such that dont provide any value to other apps or even to the user outside the context of the app like certain media (audio, images, etc.) files downloaded by a game.

    So this way you can save you data in form files in your device. Hope this will help you, feel free to ask if you have any query regarding.

    Thanks :)

    Refrences

    http://codetheory.in/android-saving-files-on-internal-and-external-storage/

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: