Tuesday, 19 September 2017

Storage In android Tutorials with Example

Storage In android Tutorials with Example

Types Of Storage with Example :

  • Shared Preferences
  • Internal Storage
  • External Storage 

code , android code for storage , How to use storage in android ,types of Storage, Example Of storage in android.

activity_storage.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.sunshine.khengar.storage.StorageActivity">

<EditText  
    android:id="@+id/ettext"         
    android:layout_width="match_parent"         
    android:layout_height="wrap_content" />

<Button  
android:id="@+id/shareprefwrite"         
android:layout_width="wrap_content" 
android:layout_height="wrap_content"         
android:text="Share Pref Write" />

<Button  
android:id="@+id/shareprefread"         
android:layout_width="wrap_content"         
android:layout_height="wrap_content"         
android:text="Share Pref Read" />

<Button 
 android:id="@+id/internalwrite"         
android:layout_width="wrap_content"         
android:layout_height="wrap_content" 
 android:text="Internal Write" />

<Button  
android:id="@+id/internalread" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"         
android:text="Internal Read" />

<Button  
android:id="@+id/externalwrite"         
android:layout_width="wrap_content"         
android:layout_height="wrap_content"         
android:text="Externa Write" />

<Button  
android:id="@+id/externalread"         
android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Exteranal Read" />

<TextView  
android:id="@+id/textview" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" />

</LinearLayout>

StorageActivity.Java

package com.sunshine.khengar.storage;

import android.content.SharedPreferences;
import android.os.Environment;
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.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class StorageActivity extends AppCompatActivity {

private static final String PREF_FILE = "Storage" ;
private static final String SCORE_STRING = "score_string" ;
private static final String SCORE = "score";
EditText ettext;
TextView textView;
Button sharedprefwrite, getSharedprefread,internalwrite;
    Button internalread,externalwrite,externalread;

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

ettext = (EditText) findViewById(R.id.ettext);
textView = (TextView) findViewById(R.id.textview);
sharedprefwrite = (Button) findViewById(R.id.shareprefwrite);
getSharedprefread = (Button) findViewById(R.id.shareprefread);
internalwrite = (Button) findViewById(R.id.internalwrite);
internalread = (Button) findViewById(R.id.internalread);
externalwrite = (Button) findViewById(R.id.externalwrite);
externalread = (Button) findViewById(R.id.externalread);

sharedprefwrite.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {

writePreference();
}
});

getSharedprefread.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
readPreference();
}
});

internalwrite.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
writeInternal();
}
});

internalread.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
readInternal();
}
});

externalwrite.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
externalWrite();
}
});

externalread.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
externalRead();
}
});
}

private void externalRead() {

File file = Environment.getExternalStorageDirectory();
File myFile = new File(file, PREF_FILE);

try {
FileInputStream fileInputStream = new FileInputStream(myFile);
byte[] b = new byte[fileInputStream.available ()];
fileInputStream.read(b);
String msg=new String(b);
textView.setText(msg);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// if (ettext != null) {// 
textView.setText(textView);//        } else {//            textView.setText("No Data");//        }

}

private void externalWrite() {
String msg = ettext.getText().toString();
File externalStorage = Environment.getExternalStorageDirectory();
File file = new File(externalStorage,PREF_FILE);

if (!file.exists())
{
file.mkdir();
}
file=new File(file,"TEST.TXT");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(msg.getBytes());
fos.close();
Toast.makeText(this, msg , Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void readInternal() {

try {
FileInputStream fis = openFileInput(PREF_FILE);
byte[] b = new byte[fis.available ()];
fis.read(b);
String msg=new String(b);
textView.setText(msg);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void writeInternal() {

String msg = ettext.getText().toString();
try {
FileOutputStream fos = openFileOutput(PREF_FILE,MODE_PRIVATE);
fos.write(msg.getBytes());
fos.close();

Toast.makeText(this, "Finish", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void readPreference() {
SharedPreferences sharedPreferences = getSharedPreferences(PREF_FILE,MODE_PRIVATE);
// int score = sharedPreferences.getInt(SCORE, 0); 
 String score_string=sharedPreferences.getString(SCORE_STRING," ");
textView.setText(score_string);
// textView.setText(score); }

private void writePreference() {
String score = ettext.getText().toString();
// int sc = Integer.parseInt(score);
SharedPreferences sharedPreferences = getSharedPreferences(PREF_FILE,MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
// editor.putInt(SCORE,sc); editor.putString(SCORE_STRING,score);
editor.commit();

Toast.makeText(this, "Done !!!", Toast.LENGTH_SHORT).show();
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 package="com.sunshine.khengar.storage">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true" android:icon="@mipmap/ic_launcher" 
 android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round" 
 android:supportsRtl="true"        android:theme="@style/AppTheme">
<activity android:name=".StorageActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Post a Comment

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search