Android App Development With AndroidCource: Internal Storage
Showing posts with label Internal Storage. Show all posts
Showing posts with label Internal Storage. Show all posts

Friday, 15 September 2017

Android Internal Storage

Internal Storage


Store private data on the device memory.

Save files directly on the device's internal storage.

By default, files saved to the internal storage are private to your application and other applications cannot access the user.

Create and write a private file to the internal storage

Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream.

Write to the file with write().

Close the stream with close().

Methods of Internal Storage


getFilesDir()

Gets the absolute path to the filesystem directory where your internal files are saved.

getDir()

Creates (or opens an existing) directory within your internal storage space.

deleteFile()

Deletes a file saved on the internal storage.

fileList()

Returns an array of files currently saved by your application.

Monday, 28 August 2017

Internal Storage Example in Android

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

import android.os.Message;
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.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";

    @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) {

                String msg = editText.getText().toString();
                String file_name = "bhaumik";

                try {
                    FileOutputStream fileOutputStream = openFileOutput(file_name,MODE_PRIVATE);
                    fileOutputStream.write(msg.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();
                }
            }
        });

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

               String messages;

                try {
                    FileInputStream fileInputStream = openFileInput("bhaumik");
                    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                    StringBuffer stringBuffer = new StringBuffer();

                    while ((messages = bufferedReader.readLine()) != null)
                    {
                        stringBuffer.append(messages + "\n");
                    }

                    textView.setText(stringBuffer.toString());
                    textView.setVisibility(View.VISIBLE);

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

Ads