Android Internal Storage

Android Internal Storage

Before continuing to our tutorial we clear you what is android internal storage and how it can use. Nowadays all the smartphones have their internal storage some have large up to 32 GB but some have 2-10 GB. There are two types of storage in android. One is android internal storage and other is external storage. external storage are can be accessed by using SD card, and internal storage is provided by android you can also say it Built-in memory. Android creates a folder in internal storage and any file you will save without sd card will be saved into that folder. So I think now we should start our tutorial.

Android Internal Storage

Android os provides many sorts of storage area for applications to store their data. These safe-keeping places are distributed preferences, external and internal storage, SQLite storage area, and safe-keeping via network interconnection.

In this section we will go through the internal storage area. Internal safe-keeping is the safe-keeping of the private data on these devices memory.

By default these data are are and private seen by only the application and get erased , when user erase your application.

Android Writing file To Internal  Storage

To be able to use internal safe-keeping to create some data in the record, call the openFileOutput() method with the name of the data file and the setting. The method could be private , open public etc. Its syntax is listed below

FileOutputStream fOut = openFileOutput("file name here",MODE_WORLD_READABLE);

 

The technique openFileOutput() returns an example of FileOutputStream. And that means you obtain it in the thing of FileInputStream. Then you can call write solution to write data on the file. Its syntax is listed below

String str = "data";
fOut.write(str.getBytes());
fOut.close();

 

Anroid  Reading file From Internal  Storage

To be able to read from the record you created just , call the openFileInput() method with the name of the document. An example is came back because of it of FileInputStream. Its syntax is listed below

FileInputStream fin = openFileInput(file);

From then on, you can call read solution to read one figure at the same time from the record and you’ll be able to print out it. Its syntax is listed below

int c;
String temp="";
while( (c = fin.read()) != -1){
   temp = temp + Character.toString((char)c);
}

//string temp contains all the data of the file.
fin.close();

 

In addition to the the techniques of write and close, there are other methods provided by the FileOutputStream category for better writing documents. These procedures are the following

Writing To Android Internal Storage Methods

  1. FileOutputStream(File file, boolean append)
  2. getChannel()
  3. getFD()
  4. write(byte[] buffer, int byteOffset, int byteCount)

In addition to the the techniques of read and close, there are other methods provided by the FileInputStream category for better reading data. These procedures are the following

Reading From Android Internal Storage Methods

  1. available()
  2. getChannel()
  3. getFD()
  4. read(byte[] buffer, int byteOffset, int byteCount)

Android Internal Storage Example

Here is a good android internal storage example demonstrating the utilization of internal storage area to store and read data. It creates a simple storage application which allows you to learn and write from inside storage.

To test out this android internal storage example, you can run this on a genuine device or within an emulator.

Steps     Description

  1. You use Android Studio room IDE to build an Android request under a bundle com.example.sappal.myapplication.
  2. Modify src/MainActivity.java document to include necessary code.
  3. Modify the res/structure/activity_main to include particular XML components

Run the application form and choose a working android os device and mount the application onto it and validate the results

Modified Content -> src/MainActivity.java.

package com.example.sappal.myapplication;

import android.app.Activity;
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.FileInputStream;
import java.io.FileOutputStream;

public class MainActivity extends Activity  {
   Button b1,b2;
   TextView tv;
   EditText ed1;

   String data;
   private String file = "mydata";

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

      b1=(Button)findViewById(R.id.button);
      b2=(Button)findViewById(R.id.button2);

      ed1=(EditText)findViewById(R.id.editText);
      tv=(TextView)findViewById(R.id.textView2);
      b1.setOnClickListener(new View.OnClickListener() {
         
         @Override
         public void onClick(View v) {
            data=ed1.getText().toString();
            try {
               FileOutputStream fOut = openFileOutput(file,MODE_WORLD_READABLE);
               fOut.write(data.getBytes());
               fOut.close();
               Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
            }
            catch (Exception e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
         }
      });

      b2.setOnClickListener(new View.OnClickListener() {
         
         @Override
         public void onClick(View v) {
            try {
               FileInputStream fin = openFileInput(file);
               int c;
               String temp="";
               while( (c = fin.read()) != -1){
                  temp = temp + Character.toString((char)c);
               }
               tv.setText(temp);
               Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show();
            }
            catch(Exception e){
            }
         }
      });
   }
}

Content -> res/layout/activity_main.xml


Do You Have Any Question About This Tutorial? Ask Question Now On Andro Development & Get Answer Instantly. No Signup/Login Needed


In the following source code abc shows the logo of http://www.androdevelopment.com

<?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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
      
   <TextView android:text="Internal storage" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
   
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Save"
      android:id="@+id/button"
      android:layout_alignParentBottom="true"
      android:layout_alignLeft="@+id/textView"
      android:layout_alignStart="@+id/textView" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:hint="Enter Text"
      android:focusable="true"
      android:textColorHighlight="#ff7eff15"
      android:textColorHint="#ffff25e6"
      android:layout_below="@+id/imageView"
      android:layout_alignRight="@+id/textView"
      android:layout_alignEnd="@+id/textView"
      android:layout_marginTop="42dp"
      android:layout_alignLeft="@+id/imageView"
      android:layout_alignStart="@+id/imageView" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="load"
      android:id="@+id/button2"
      android:layout_alignTop="@+id/button"
      android:layout_alignRight="@+id/editText"
      android:layout_alignEnd="@+id/editText" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Read"
      android:id="@+id/textView2"
      android:layout_below="@+id/editText"
      android:layout_toLeftOf="@+id/button2"
      android:layout_toStartOf="@+id/button2"
      android:textColor="#ff5bff1f"
      android:textSize="25dp" />
      
</RelativeLayout>

Content -> res/values/string.xml.

<resources>
   <string name="app_name">My Application</string>
</resources>

Content ->  AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sappal.myapplication" >
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

Let’s make an effort to run our Android Internal Storage App we just revised. I assume you’d created your AVD while doing android environment setup. To perform the software from Android studio room, available one of your project’s activity documents and click Run Eclipse Run Icon from the tool pub. Android studio room installs the software on your AVD and begins it and when everything is okay with your set-up and program, it’ll screen pursuing Emulator windowpane

Android Internal Storage

Now what you ought to do is to go into any text message in the field. For instance , i have joined some text message. Press the save button. The next notification seems in you AVD

Android Internal Storage

If you press the load button now, the application form will browse the data file , and display the info. In case there is our, pursuing data would be delivered

Android Internal Storage

Be aware it is possible to treat this document by turning to DDMS tabs. In DDMS , select file explorer and navigate this path.

tools>android>android device Monitor

as shown below

Android Internal Storage


Do You Have Any Question About This Tutorial? Ask Question Now On Andro Development & Get Answer Instantly. No Signup/Login Needed