Chap-5. Data Storage, Retrieval and Sharing: File System in Android

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Chap-5.

Data Storage, retrieval and Sharing


File system in android: - Most of the Android user are using their Android phone just for
calls, SMS, browsing and basic apps, but form the development prospective, we should know about
Android internal structure. Android uses several partitions (like boot, system, recovery , data etc) to
organize files and folders on the device just like Windows OS. There are mainly 6 partitions in Android
phones, tablets and other Android devices. Note that there might be some other partitions available,
it differs from Model to Model. But logically below 6 partitions can be found in any Android devices.

/boot /system/recovery /data/cache/misc

Also Below are the for SD Card Fie System Partitions. /sdcard /sd-ext

You can know which partitions are available along with the partition size for all partition in your android
device. Go through the below image and run the adb command as shown in that image.

1./boot:- This is the boot partition of your Android device, as the name suggests. It includes the
android kernel and the ramdisk. The device will not boot without this partition. Wiping this
partition from recovery should only be done if absolutely required and once done, the device must
NOT be rebooted before installing a new one, which can be done by installing a ROM that includes a
/boot partition.

2./System:- As the name suggests, this partition contains the entire Android OS. This includes the
Android GUI and all the system applications that come pre-installed on the device. Wiping this
partition will remove Android from the device without rendering it unbootable, and you will still be
able to put the phone into recovery or bootloader mode to install a new ROM.

3./Recovery:- This is specially designed for backup. The recovery partition can be considered as an
alternative boot partition, that lets the device boot into a recovery console for performing advanced
recovery and maintenance operations on it.

4./Data:- It is called userdata partition. This partition contains the user’s data like your contacts, sms,
settings and all android applications that you have installed. While you are doing factory reset on your
device, this partition will wipe out, Then your device will be in the state, when you use for he first time,
or the way it was after the last official or custom ROM installation.

5./Cache:- This is the partition where Android stores frequently accessed data and app components.
Wiping the cache doesn’t affect your personal data but simply gets rid of the existing data there, which
gets automatically rebuilt as you continue using the device.

6.Misc: -This partition contains miscellaneous system settings in form of on/off switches. These settings
may include CID (Carrier or Region ID), USB configuration and certain hardware settings etc. This is an
important partition and if it is corrupt or missing, several of the device’s features will will not function
normally.

7.SDCard:- This is not a partition on the internal memory of the device but rather the SD card. In terms
of usage, this is your storage space to use as you see fit, to store your media, documents, ROMs etc.
on it. Wiping it is perfectly safe as long as you backup all the data you require from it, to your
computer first. Though several user-installed apps save their data and settings on the SD card and
wiping this partition will make you lose all that data.

8./SD-ext:- This is not a standard Android partition, but has become popular in the custom ROM scene.
It is basically an additional partition on your SD card that acts as the /data partition. It is especially
useful on devices with little internal memory allotted to the /data partition. Thus, users who want to
install more programs than the internal memory allows can make this partition and use it for installing
their apps.

Internal and external storage: -Internal storage is used to store android app private data
such as files, cached files, shared_preferences and database files.

The first way to save files in your Android application is to write to the device’s internal storage.

To save text into a file, you use the FileOutputStream class. The openFileOutput() method opens a
named file for writing, with the mode specified. In this example, you used the
MODE_WORLD_READABLE constant to indicate that the file is readable by all other applications.
MODE_PRIVATE

MODE_APPEND

MODE_WORLD_WRITEABLE

To convert a character stream into a byte stream, you use an instance of the OutputStreamWriter class,
by passing it an instance of the FileOutputStream object:

You then use its write() method to write the string to the file

To ensure that all the bytes are written to the file, use the flush() method.

Finally, use the close() method to close the file.

Example:

package com.example.sairamkrishna.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){

});

External Storage: - Android external storage can store files in SD card or device build-in flash
disk memory. Internal and external is only a logical classification.

Every Android device supports a shared "external storage" space that you can use to save files. This
space is called external because it's not a guaranteed to be accessible—it is a storage space that users
can mount to a computer as an external storage device, and it might even be physically removable (such
as an SD card). Files saved to the external storage are world-readable and can be modified by the user
when they enable USB mass storage to transfer files on a computer. Most often, you should use external
storage for user data that should be accessible to other apps and saved even if the user uninstalls your
app, such as captured photos or downloaded files. The system provides standard public directories for
these kinds of files, so the user has one location for all their photos, ringtones, music, and such.

1. package example.javatpoint.com.externalstorage;  
2.
3. import android.support.v7.app.AppCompatActivity;  
4. import android.os.Bundle;  
5. import android.view.View;  
6. import android.widget.Button;  
7. import android.widget.EditText;  
8. import android.widget.Toast;  
9.   
10. import java.io.BufferedReader;  
11. import java.io.File;  
12. import java.io.FileInputStream;  
13. import java.io.FileNotFoundException;  
14. import java.io.FileOutputStream;  
15. import java.io.IOException;  
16. import java.io.InputStreamReader;  
17. import java.io.OutputStreamWriter;  
18.   
19. public class MainActivity extends AppCompatActivity {  
20.     EditText editTextFileName,editTextData;  
21.     Button saveButton,readButton;  
22.     @Override  
23.     protected void onCreate(Bundle savedInstanceState) {  
24.         super.onCreate(savedInstanceState);  
25.         setContentView(R.layout.activity_main);  
26.   
27.         editTextFileName=findViewById(R.id.editText1);  
28.         editTextData=findViewById(R.id.editText2);  
29.         saveButton=findViewById(R.id.button1);  
30.         readButton=findViewById(R.id.button2);  
31.   
32.         //Performing action on save button  
33.         saveButton.setOnClickListener(new View.OnClickListener(){  
34.   
35.             @Override  
36.             public void onClick(View arg0) {  
37.                 String filename=editTextFileName.getText().toString();  
38.                 String data=editTextData.getText().toString();  
39.   
40.                 FileOutputStream fos;  
41.                 try {  
42.                     File myFile = new File("/sdcard/"+filename);  
43.                     myFile.createNewFile();  
44.                     FileOutputStream fOut = new FileOutputStream(myFile);  
45.                     OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);  
46.                     myOutWriter.append(data);  
47.                     myOutWriter.close();  
48.                     fOut.close();  
49.                     Toast.makeText(getApplicationContext(),filename + "saved",Toast.LENGTH_LON
G).show();  
50.                 } catch (FileNotFoundException e) {e.printStackTrace();}  
51.                 catch (IOException e) {e.printStackTrace();}  
52.             }  
53.         });  
54.   
55.         //Performing action on Read Button  
56.         readButton.setOnClickListener(new View.OnClickListener(){  
57.             @Override  
58.             public void onClick(View arg0) {  
59.                 String filename=editTextFileName.getText().toString();  
60.                 StringBuffer stringBuffer = new StringBuffer();  
61.                 String aDataRow = "";  
62.                 String aBuffer = "";  
63.                 try {  
64.                     File myFile = new File("/sdcard/"+filename);  
65.                     FileInputStream fIn = new FileInputStream(myFile);  
66.                     BufferedReader myReader = new BufferedReader(  
67.                             new InputStreamReader(fIn));  
68.                     while ((aDataRow = myReader.readLine()) != null) {  
69.                         aBuffer += aDataRow + "\n";  
70.                     }  
71.                     myReader.close();  
72.                 } catch (IOException e) {  
73.                     e.printStackTrace();  
74.                 }  
75.                 Toast.makeText(getApplicationContext(),aBuffer,Toast.LENGTH_LONG).show();  
76.             }  
77.         });  
78.     }  
79. }  

File Management tools:- Android got a native file manager in Android 6.0 Marshmallow. It’s
pretty barebones, but it gets the job done for basic file management.

1. Bluetooth.
2. USB Cable.
3. ShareIt.
4. Pendrive.
5. Phone Internal Memory And External Memory (SD Card).
6. Android App.

You might also like