working with internal storage andriod

Working with internal storage – Android 2016

Hey, guys. In this post we are going to see how internal storage in android works. We will create a simple application which i have called working with internal storage. In this application we have an editText where we write some text and hit the save button to save that text and on the Second activity we can get the save text. So let’s get started

Working with internal storage android – Demo

Project structure

working with internal storage android project structure

activity_main.xml




In this activity we have a editText and 2 button one to save data & second to navigate to second activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.hackerkernel.workingwithinternalstorage.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:hint="Enter info to save"
        android:id="@+id/editText" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/save"
        android:layout_below="@+id/editText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to b"
        android:id="@+id/gotob"
        android:layout_below="@+id/editText"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


</RelativeLayout>

activity_second.xml

In this activity we have 1 button to display the save data from main activity and a textView where data will be displayed.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.hackerkernel.workingwithinternalstorage.SecondActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load"
        android:id="@+id/load"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_below="@+id/load"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>



MainActivity.java

Firstly we have instantiate the views(EditText & Button) and we are firing the saveData(); method whenever someone click the save button.
Inside saveData(); method firslty we will get the text from the editText and store it in a String variable then we will convert the string which has the text in a Bytes array and call the openFileOutput(); method to store file

package com.hackerkernel.workingwithinternalstorage;

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

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    EditText editText;

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

        editText = (EditText) findViewById(R.id.editText);
        Button save = (Button) findViewById(R.id.save);
        Button gotob = (Button) findViewById(R.id.gotob);

        //Save button is clicked
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData();
            }
        });

        //Go to B is clicked
        gotob.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,SecondActivity.class));
            }
        });
    }

    public void saveData(){
        String text = editText.getText().toString();
        byte[] bytes = text.getBytes();

        File file;

        FileOutputStream fos = null;
        try {
            fos = openFileOutput("husain.txt", Context.MODE_PRIVATE);
            fos.write(bytes);

            //File directory
            file = getFilesDir();
            Toast.makeText(getApplicationContext(),"File save "+file,Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //close FileOutputstream
            try {
                if (fos != null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

SecondActivity.java

We ever we click the load Button. loadData(); method is fired.
Inside loadData(); method we will call openFileInput(“filename.txt”); method and give the filename as pramater and then we will loop throught the file bit by bit and store them in a StringBuilder. NOTE: file are stored in bits so we will convert them to character by casting them to char

package com.hackerkernel.workingwithinternalstorage;

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

import java.io.FileInputStream;
import java.io.IOException;

public class SecondActivity extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        textView = (TextView) findViewById(R.id.textView);
        Button load = (Button) findViewById(R.id.load);

        load.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadData();
            }
        });
    }

    private void loadData() {
        FileInputStream fis = null;
        try {
            fis = openFileInput("husain.txt");
            int read;
            StringBuilder sb = new StringBuilder();
            while ((read = fis.read()) != -1){
                sb.append((char)read);
            }

            textView.setText(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *