Showing posts with label Android Storage. Show all posts
Showing posts with label Android Storage. Show all posts

Thursday, 3 November 2016

ExternalStorage

Each Android-perfect gadget bolsters a common "outer stockpiling or External Storage" that you can use to spare records. This can be a removable stockpiling media(Storage media), (for example, a SD card) or an inward (non-removable) capacity. Records spared to the outer stockpiling are world-intelligible and can be changed by the client when they empower USB mass stockpiling to exchange documents(transfer files) on a PC.

Access to external storage  i.e Read or Write files.
In Android manifest.xml we have to include the code.
<manifest>
   - - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -
   - - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -
<uses-permission android : name ="android.permission.WRITE_EXTERNAL_STORAGE/>
<uses-permission android : name ="android.permission.READ_EXTERNAL_STORAGE/>
- - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -

</manifest>

In MainActivity.java we have to write code for external storage below shown how it is,

                          //WRITE FILE //
public boolean isExternalStorageWritable() {
    String externalstate = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(externalstate)) {
        return true;
    }
    return false;
}

                         //READ FILE //
public boolean isExternalStorageReadable() {
    String externalstate = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(externalstate) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(externalstate)) {
        return true;
    }
    return false;
}

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


Monday, 31 October 2016

SharedPreferences

The SharedPreferences class gives a general system that permits you to spare and recover determined key-esteem sets of primitive information sorts. You can utilize SharedPreferences to spare any primitive information: booleans, coasts, ints, yearns, and strings. This information will continue crosswise over client sessions (regardless of the possibility that your application is executed).

There are two methods of SharedPreferences.

1) get SharedPreferences()
2)get Preferences()

 Example Code for  SharedPreferences.

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="#be2f2f">

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_marginTop="20dp" 
 android:background="#f3e8e8" 
 android:allowUndo="false" 
 android:hint="Name" />

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText2" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" 
 android:background="#e8dfdf" 
 android:hint="Location" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Save" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Show" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button2" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

showactivity.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" android:layout_width="match_parent" 
 android:layout_height="match_parent">

<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:id="@+id/textView" 
 android:text="Large Text" 
 android:layout_gravity="center_horizontal" />

<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:id="@+id/textView2" 
 android:text="Large Text" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>
 
MainActivity.java :-

package com.kiranapp;

import android.content.Intent;
import android.content.SharedPreferences;
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.Toast;

public class MainActivity extends AppCompatActivity {
    Button savebtn,showbtn;
    EditText uname,uloc;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 savebtn=(Button)findViewById(R.id.button);
 uname=(EditText)findViewById(R.id.editText);
 uloc=(EditText)findViewById(R.id.editText2);
 showbtn=(Button)findViewById(R.id.button2);
 savebtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 String s1=uname.getText().toString();
 String s2=uloc.getText().toString();
 SharedPreferences sp=getSharedPreferences("myfile",MODE_PRIVATE);
 SharedPreferences.Editor e=sp.edit();
 e.putString("name",s1);
 e.putString("loc",s2);
 e.commit();
 Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
 }
 });
 showbtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 Intent in=new Intent(MainActivity.this,ShowActivity.class);
 startActivity(in);
 }
 });
}
}

ShowActivity.java :-

package com.kiranapp;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

/** * Created by Dell on 10/31/2016. */
 public class ShowActivity extends AppCompatActivity {

    TextView t1, t2;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.showactivity);
 t1=(TextView)findViewById(R.id.textView);
 t2=(TextView)findViewById(R.id.textView2);
 SharedPreferences sp=getSharedPreferences("myfile",MODE_PRIVATE);
 String s1=sp.getString("name",null);
 String s2=sp.getString("loc",null);
 t1.setText(s1);
 t2.setText(s2);
 }
}
 

Output :-

























Android Storage

Android gives a few choices to you to spare determined application information. The arrangement you pick relies on upon your particular needs, for example, whether the information ought to be private to your application or open to different applications (and the client) and how much space your information requires.

There are five different types of android storage.

1) Shared Preferences :-

Store private primitive information in key-esteem sets.

2) Internal Storage :-

Store private information on the gadget memory.

3) External Storage :-

Store open information on the mutual outside capacity.

4) SQLite Databases :-

Store organized information in a private database.

5) Network Connection :-

Store information on the web with your own particular system server.

Android gives an approach to you to uncover even your private information to different applications — with a substance supplier. A substance supplier is a discretionary segment that uncovered read/compose access to your application information, subject to whatever limitations you need to force. For more data about utilizing content suppliers, Content Providers documentation.