Share your knowledge

Saturday 11 March 2017

Android Internal Storage Example

There are several ways(Shared preferences, Internal storage, External storage, SQLite) to save data in android. Here we will discuss about Internal Storage.

Note: Saving files to the internal storage are private to the application, and other applications will not have access to these files.

Output will look like below:



Download code from here: internal storage

1. Create a project and name it as Internal Storage

2. Open activity_main.xml and paste below code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.internalstorage.MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:fontFamily="monospace"
        android:hint="Enter some data here"
        android:paddingLeft="10dp"
        android:textStyle="bold" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText"
        android:layout_marginTop="20dp"
        android:gravity="center">

        <Button
            android:id="@+id/write"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="write" />

        <Button
            android:id="@+id/read"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/write"
            android:gravity="center"
            android:text="read" />
    </RelativeLayout>

</RelativeLayout>

3. Open MainActivity.java and paste below code



package com.internalstorage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private EditText enteredData;
    private Button write, read;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        enteredData = (EditText) findViewById(R.id.editText);

        write = (Button) findViewById(R.id.write);
        read = (Button) findViewById(R.id.read);

        write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (enteredData.getText().toString().length() > 0) {
                    writeData(enteredData.getText().toString());
                } else {
                    showMessage();
                }

            }
        });


        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                enteredData.setText(readData());
            }
        });
    }

    private void showMessage() {
        Toast.makeText(MainActivity.this, "Please enter some data", Toast.LENGTH_SHORT).show();
    }

    private void writeData(String data) {

        try {
            //Using FileOutputStream we can write data to the specified file
            //openFileOutput method will take file name and mode as parameters, and return instance of fileOutputStream

            //openFileOutput method is used to create and save a file
            FileOutputStream fileOutputStream = openFileOutput("notes", MODE_PRIVATE);
            fileOutputStream.write(data.getBytes());
            fileOutputStream.close();

            Toast.makeText(MainActivity.this,"Data saved successfully",Toast.LENGTH_SHORT).show();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private String readData() {

        int c;
        StringBuilder data = new StringBuilder();
        try {
            //openFileInput method is used to open and read a file
            FileInputStream fileInputStream = openFileInput("notes");
            //checking for end of file
            while ((c = fileInputStream.read()) != -1) {
                data.append(Character.toString((char) c));
            }
            fileInputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return data.toString();
    }
}








No comments:

Post a Comment