External Storage Example in Android |Android App Development With AndroidCource

Monday 28 August 2017

External Storage Example in Android

Add Permission in AndroidMainfest.xml File

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    android:orientation="vertical"
    tools:context="com.example.bhaumik.externalstorage.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:layout_marginTop="10dp"
        android:hint="Enter a Message"
        android:gravity="top"
        android:textSize="18dp"
        android:id="@+id/et_message"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Write Message"
        android:id="@+id/btn_write"
        android:textAllCaps="false" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Read Message"
        android:textAllCaps="false"
        android:id="@+id/btn_read"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="10dp"
        android:hint="Show Message"
        android:textSize="18dp"
        android:id="@+id/tv_show"/>

</LinearLayout>
MainActivity.java

package com.example.bhaumik.externalstorage;

import android.os.Environment;
import android.renderscript.ScriptGroup;
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.TextView;
import android.widget.Toast;

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.InputStreamReader;

public class MainActivity extends AppCompatActivity {

    EditText editText;
    TextView textView;
    Button write,read;

    String file_name = "bhaumik.txt";

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

        editText = (EditText) findViewById(R.id.et_message);
        textView = (TextView) findViewById(R.id.tv_show);
        write = (Button) findViewById(R.id.btn_write);
        read = (Button) findViewById(R.id.btn_read);
        textView.setVisibility(View.GONE);

        write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // Check External Storage is Available or Not on the Device

                String states = Environment.getExternalStorageState();

                if(Environment.MEDIA_MOUNTED.equals(states))
                {
                    // External Storage Get Root
                    File root = Environment.getExternalStorageDirectory();

                    //Create Folder/Directory on the External Storage
                    File dir = new File(root.getAbsolutePath() + "/MyFiles");

                    //Check Folder/Directory is Exists or Not

                    if(!dir.exists())
                    {
                        dir.mkdir();
                    }

                    File file = new File(dir,file_name);

                    String message = editText.getText().toString();

                    try {
                        FileOutputStream fileOutputStream = new FileOutputStream(file);
                        fileOutputStream.write(message.getBytes());
                        fileOutputStream.close();
                        Toast.makeText(MainActivity.this,"Message Saved..",Toast.LENGTH_SHORT).show();
                        editText.setText("");

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                else
                {
                    Toast.makeText(MainActivity.this,"SD Card Not Found",Toast.LENGTH_SHORT).show();
                }
            }
        });

        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // External Storage Get Root
                File root = Environment.getExternalStorageDirectory();

                //Create Folder/Directory on the External Storage
                File dir = new File(root.getAbsolutePath() + "/MyFiles");

                File file = new File(dir,file_name);

                String msg;

                try {

                    FileInputStream fileInputStream = new FileInputStream(file);
                    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                    StringBuffer stringBuffer = new StringBuffer();

                    while ((msg = bufferedReader.readLine()) != null)
                    {
                        stringBuffer.append(msg + "\n");
                    }
                    textView.setText(stringBuffer.toString());
                    textView.setVisibility(View.VISIBLE);

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

0 comments:

Post a Comment