Thursday 3 November 2016

InternalStorage

Android Internal storage is the capacity of the private information on the gadget memory. Naturally, sparing and stacking documents to the inner stockpiling are private to the application and different applications won't have entry to these records. At the point when the client uninstalls the applications the inward put away documents connected with the application are additionally expelled. Nonetheless, take note of that a few clients root their Android telephones, picking up superuser get to. These clients will have the capacity to peruse and compose whatever records they wish.

For example as shown figure is internal storage in android.

InternalStorage
InternalStorage Figure


code for internal storage in android

activity_main.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:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 tools:context="com.kiranapp.MainActivity" 
 android:background="#f3f0f0">

<TextView 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Welcome to internal storage in android" 
 android:id="@+id/textView" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" />

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Text to write" 
 android:layout_alignTop="@+id/button2" 
 android:id="@+id/button" 
 android:onClick="click text" 
 android:layout_gravity="center_horizontal" />

<Button
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Read from file" 
 android:onClick="click read" 
 android:id="@+id/button2" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

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.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {
    EditText editText;
   // TextView t1;
  //  Button b1,b2;
    static final int READ_BLOCK_SIZE = 100;
@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
// t1=(TextView)findViewById(R.id.textView);
// b1=(Button)findViewById(R.id.button);
// b2=(Button)findViewById(R.id.button2);
 editText=(EditText)findViewById(R.id.editText);
 }

public void WriteBtn(View v) {
try {
FileOutputStream fileout=openFileOutput("file.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(editText.getText().toString());
outputWriter.close();
Toast.makeText(getBaseContext(), "saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void ReadBtn(View v) {
try {
FileInputStream fileIn=openFileInput("file.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);
char[] inputBuffer= new char[READ_BLOCK_SIZE];
String s="";
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
editText.setText(s);
}
catch (Exception e) {
e.printStackTrace();
 }
}
}

Output :-

InternalStorage
InternalStorage Fig :1

















  


InternalStorage
InternalStorage Fig :2


No comments:

Post a Comment