Tuesday, 1 August 2017

Save File in Internal Storage in Android


In Android, we have to way to store data.

1.) Internal storage
2.) External storage

In the below example we use internal storage to store data and retrieve date from the same location.I am using internal storage so we don't need for permission to access internal storage.But when we use external storage we need permission to read or write on external storage.In this example on click of save button value of edit-text saved in internal storage and value retrieve on click of a retreive button.






Step 1: -Open your project and in activity_main.xml create the design of your own requirement.To give you the idea I am creation simple design.      

activity_main.xml  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.hp.mysavefile.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <EditText
        android:id="@+id/entername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/save"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="SAVE"
        android:onClick="savedata"/>
        <Button

            android:id="@+id/retreive"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="RETRIEVE"
            android:onClick="retreivdata"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/display"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="15sp"/>
    </LinearLayout>

</LinearLayout>


Step 2: -Open your MainActivity.java and write code MainActivity.java I am writing code for save button and retrieve button.

MainActivity.java
package com.example.hp.mysavefile;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
 TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        display=(TextView)findViewById(R.id.display);

    }
                public void savedata(View view){
                EditText name=(EditText)findViewById(R.id.entername);
                String username=name.getText().toString();
                try {
                    FileOutputStream outputStream=openFileOutput("amit.txt", Context.MODE_PRIVATE);
                    outputStream.write(username.getBytes());
                    outputStream.close();

                    name.setText("");
                }
                catch (FileNotFoundException e){
                    e.printStackTrace();

                }

                catch (IOException e){
                    e.printStackTrace();
                }
            }
            public void retreivdata(View view) {
                try {
                    FileInputStream inputStream = openFileInput("amit.txt");
                    if (inputStream != null) {
                        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                        String temp, temp1 = "";
                        try {
                            while ((temp = bufferedReader.readLine()) != null)
                                temp1 += temp;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        display.setText(temp1);
                        inputStream.close();
                    }
                }
            catch (Exception e) {
            e.printStackTrace();
            }


                        }
                    }








                                                                 


No comments:

Post a Comment

Change Fragment on button click in Android

In example mention below on click of a button, we attach the desired fragment in our frame layout which was present in activity_main.xml. ...