 Very good afternoon to all of you. In this session, I'm going to cover about the local storage options of Android. In Android, we have some storage options. So that is exactly where we can say that is the data storage options we have in Android. Now, using these options, we can store some data into database. We can read some data from the database. And if we want to store some data into file, or if we want to create some file and store that file into some SD card or internal memory of your device. So in using this data storage, we can do this kind of things. Now, the storage options in Android, we have that is shared preferences, creation and storage of arbitrary file type, means we can create some file, and on that file, you can store some strings or text. And SQLite relational database. Android exactly support this SQLite, internal support this SQLite database, and will store into your device. Now, Android provides several options for you to save persistent application data. So when I'm talking about persistent application data, in the senses, you are saving your data in your devices. Using this shared preferences, internal, external memory, or with the help of file. And you can use the SQLite database to store some information. Now, storage option. Now, suppose you want to store some information, so you have to decide what kind of storage you required. Depends on your need, the solution you have to find. Solution in the senses, suppose your data, you don't want to share your data, then you can use this SQLite database to store that data. If you want to share some data, then you can share your external and internal storage to access that data from or save that data into external storage, internal storage. So you have to take care of these three things. Data should be private to your application, then accessible to other application. This accessible to other application in the senses, you are accessing internal memory as well as or external memory. Then how much space your data is required, based on if suppose your data size is 2, 3 MB or 30, 40 MB, then suppose if your data size is 2, 3 MB, then you can use internal storage. If the data size is suppose 30, 40 MB, then you can use your external storage. Now, we'll start with the very first storage option, that is shared preferences. Basically, shared preferences is used when we require to save some persistent data. In this, each preference has a key and value pair. The key means your strings, and individual means the value is what is your any kind of value you can use, the bull and long, float, int, that kind of value you can use in this shared preferences. I'm repeating this because this is very important is each preference is a simple key and value pair. Your key is your string, and a value could be anything, bull and long, int, float. Now, shared preferences, here the manage is through, we can use the shared preferences via Java code, or we can use with the spatial activity, preference activity. So that is your XML. Now, after that you're stored encrypted, you have to store whatever the value you're going to store, that is in a form of unencrypted on your disk. Then how we can create preferences in Java. So shared preferences class represents set of preferences. So those preferences, we have to initiate with a mode, that is private mode. There are lots of mode you can use, such as you can see here the mode writable. But sharing preferences between is, but we can share our preferences, the preference data, with your activity to activity. But if you want to share that data with application, it is not possible in this, using preferences. So for creating preferences in Java, you can use preferences for current activity. So you can write this way, the private shared preferences, then they have to create object of that, then is equal to get preferences context.mode private. Then another way you can use that is the name set of your preferences. Here you will set the name of your preference. For that, private shared preferences, then the object is equal to your get shared preferences, then you have to write a preference name, then your mode, that is private mode. Using this, you can create your preference. Now, here when I'm using preferences, so we have to create preference activity. So preference activity defines and write value on the disk automatically. Then value can be your string Boolean or list of string. As we told you in the very first slide, our preferences has a key or pair value. So here I'm talking about value, that is, it could be in string Boolean and long in any data type. Now defining in a layout XML, you can also define your preferences in XML. That is your preference. You can say that preference.xml. Next is, that is, navigate to preference activity with an intent object. You can navigate your preferences using intent. Now we'll see one simple application based on shared preferences. So this is my app of our shared, Android shared preference. So you can see that here I have created one layout that is main.xml. Inside that layout, I have used some text views, edit text, buttons. So here for the shared preferences, I have used two edit text. This first edit text is for the MEM1. In this, we are going to save first preference. And this is for the second. Then there is two edit text. Whatever the value I'm going to write here, it will display in this text views. So if I'm writing here in first edit text, it will display here. And whatever the value I'm writing here, it will display in this text view after clicking these two buttons. That is first button name is save MEM1. And next is save MEM2. So now first, if we want to perform this logic using shared preferences, then we have to create Java file. And first we have to access these widgets. So we'll go to that Java file. You can see here I have created two edit text object and two text view objects. And same here for the button also I've created two object for the button. And after that, using this fine view by ID, I'm accessing those text views, edit text, and button. Where are their IDs? Then I have to perform some, I want to generate some event, I have to perform some event via this button. So I have to set the onclick listener method to this button. After that I'm loading that preference. When I'm clicking on button, suppose I'm clicking button 1, that is button save MEM1, then it will execute this body. And this body, what I'm doing, I'm just creating the string of a name of a preference. And I'm getting that text of your edit text. And then after using this save preference method, I'm calling this method here. And I'm storing that value into key and value pair. And after that, I'm committing that editor. And then after that, I'm calling the load preferences. Inside the load preferences, I'm copying those, the key value into your strings. So you can see here inside the MEM1 and MEM2, I have stored those value. In MEM1, I'm getting the text of edit text 1. And then I'm calling this load preferences. So whatever the value is in your MEM1, it will store in your string. And then after storing that value into string, I'm displaying those values into the text view by using set text. Same operation we can perform for the button 2. I've also used this set on click listener for button same MEM2. And then it will directly call here. Then it will take the value of edit text 2. And I'm calling save preferences. And again, I'm storing those values into key value pair. And then I'm calling the load preferences. This load preferences will take that value into string. And after that is string 2, I'm setting second value into text view 2. So now we'll run this application. And we'll see the output. This is Android shared preference editor. So suppose here I'm writing hello. And here I'm writing students. When I'm clicking on save MEM1, it will display here. When I'm clicking save MEM2, it will display here. Then go back. Again, if I'm clicking on that shared preferences, this is my application, I'll get that value there. So when I'm entering next time, hello, teachers. And I click on this hello will display here. And teachers will display here. Now to delete the value of your text view 1, text view 2, or if you want to erase that value from the shared preferences, then you have to follow the steps. You just hold this and go to the app info. And after that, just click on clear data. Go to that application. Again, run that application. Now you can see your data is cleared from your text views, means from your preferences. This is the way you can use shared preferences by Java. Now we'll move to another application. That is your internal file storage. So your file storage is what? First here you are going to create some file and you are going to store that value onto that particular file. For creating file, you have to keep in mind two things. First you have to give your extension name and the name of the file and the extension of the file. So here file can be created and read on a persistent media. So we can create one file and we can store that file into your internal or external storage. And we can read that file from those internal or external storage. And then what kind of file we can create here? We can read image file or a TXT file, XML file. This kind of file we can read from the internal or external memory. Then file can be designated for internal or external storage. That means it is local to your app. Your application can be access your files from the internal or external storage. The internal first, what is that internal storage? The Android can save file directly to the device. That is internal storage. Then if you're saving your file into your device only, then it will be saving your file into your internal storage. This file is private to the application and will be removed if uninstalled the application. Suppose if you have installed your application into your device and after that you are uninstalling your application so that particular file will be deleted from your device. We can create a file using open file output with the parameter and file name and the operating mode. Again here we have to get the operating mode for your file. It should be private or readable for the world. As we have used in shared preferences. Then if we want to open our file, at that time we can use open file input, passing the parameter as the file name. One thing that we need to remember is give to extension of the file. As I said, you have to give the extension of your file name. It is optional. If you want to give, you can give otherwise. It will bi-defile, it will take as a text file. Now we will see some simple example based on internal storage example. So here I have created one internal storage application. We will see this application. So first we will go to the layout. In layout there is a main file, main.xml. Now again here I have used two text box, added text and two text views and one button. When I am here, you can see I have used here enter file name. So here in this text box I am going to enter name of your file and enter content. Here I am going to enter the content of your file. And when I am clicking save button, it will save into your internal memory with the file name and your content. So and first for that again to perform this logic, we have to write a code, java code. So first we have to access those added text and button. So here I have created two objects for the added text and one object for the button. Here I have access using find view by ID in the file name, using the file name ID of the first added text. And then I have access again second one that is find view by ID. And the file name of your second added text. And same for the button I have access using find view by ID and the ID of your button. Then I am giving the set on click listener to the button because I want to save that file and your content of your file. Then when I am clicking on that button, so this particular operation will perform. First I am getting the text of added text into this string, this file name string and another into your content. So I am taking the value of this two, get the value of two added text into these two strings. And after that I am creating file output stream. Then after that I am opening that file output. We can enter the file name and the context. We have to give the mode of our file. That is mode is private or readable for the world. And then we have to write using this method. I am writing the content of content into your file. And I am closing that file. If everything is proper then we will get the message, the file name, whatever the file name we are going to give, that file name saved. This message we will get. Otherwise it will throw the exceptions. Now we will see that first we will run that application and we will see the output. Go to your application, internal storage, Android application. You can see this is my output. So here first we have to enter the file name. I am entering hello dot txt. And I am writing the content. Good afternoon. And I am saving this content and file name into an internal storage space. I am sorry. So when I am clicking this we will get the message hello dot txt saved. Now our file is saved or not. For that we have to go to that, we have to go to the ddms. And from there we have to check that file. So this ddm will show the emulator. So we will go to the ddms. Inside the ddms we have to go to data. Inside the data we have to go to again data folder. Then the package of our application. So what is our package? Comm.exercise, Android internal storage. Comm. So we will go there. Comm.exercise, Comm.Android internal storage. On that you can see here there is a file folder. Open this file folder. You can see here I have created the hello dot txt. So if you want to see this content of hello dot txt, just select this. Here there is a push file, sorry pull file. So we can pull this file from device to our system. Click on this, save into your desktop, check that. Actually it is not pulling from your ddms. But you can see that here the hello text txt file is created. And inside that we will get that output. So let us again create one file, hello, then nullish, save this, hello saved, we will go to that. You can see here hello, file is there, pull this into, I am pulling this new folder, failed the pull selections. So actually it is not pulling from your device but you can see here I have created that file and surely we will get that string that I have entered in this, we will get that file. So this process you can follow and you can open your file. So we will move to another storage option. So another storage option is external storage. Another storage in the sense you have a SD card. So every Android compatible device supports shared external storage that you can use to save files. This external storage you can use to save text file, PDF, any kind of file you can save in this external storage. I mean normally in each smartphones we will get this option for the sharing, inserting that SD card for the external memory. And this can be removable storage media, you know that SD card is obviously is a removable media you can insert, you can insert and when you card then you can insert, we do not require then you can remove from your mobile phones or tablet. Then file saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer file on a computer. So you can see whenever you are using SD card, the content of your SD card, whatever you have stored in your SD card, anyone can access that using your tablet or mobile phone or you can use also with some card reader. Now we will see the external storage example. You have created one external storage of application, inside the RES I have a layout, I am just opening that layout folder, you can here you can see I have used one image view widget to display the images and I have access this image view from your java file is very simple there are two, three line of code to display the image, you can see here only three line of code we can display image. So this I am accessing that find view by ID, I am accessing this image view using the find view by ID with your image ID. Then I am using bitmap class and inside bitmap I am displaying that I am giving the path of your image where you have stored. So now you have to check that this path, if you are using a device, if you are using a Akash tablet. So inside that the path is your MNT, EXT, SD and the whatever your file name. So here my file name is error1.jpeg. So and when you are using this emulator, so I can show the path of emulator that is go to DDMS and then you have to go to MNT then SD card. So here your external storage name is the SD card, some of the same path you will get in some of the devices but in Akash tablet you have to use MNT, EXT, SD. So I will run this application and I will show how I will get that output in Akash tablet. So first I have to copy one image that is this image into my EXT Akash tablet and then I will run that application, image EXT, image SD card. So first we have to run this. So here you can see there is no image because I have given the path of my Akash tablet SD card or here I have a path of that is MNT SD card but in Akash we are using MNT, EXT, SD. So I have given the Akash path so it will not get any image here. So here the SQLite is database, in your Android this is exactly the by default database we can use and it is a structure data, structure data can be saved into your SQLite database Android provide built-in support in this, all classes and interfaces are in your, if you want to use, if you are using this SQLite then you have to use this android.database.sql lite package. This package you have to give by default when you are using SQLite database. Now but there is a some, it is a very lite database so it cannot hold the maximum size of your data. So we will see the simple example based on your SQLite. So inside the SQLite database, database file are local to the app whatever if you are creating a database it will local to your app. If you uninstall your application it will uninstall, it will remove from your device. To share structure data with the other apps you have to use content provider, custom content provider to share your data to the other apps. Now we will see the simple example based on SQLite database. Here the database sample have one application and first we will see the layout of this application. So this is our main layout, I have created here three layout. One layout for the checking data means retrieving data and displaying and another is to store that data into a database. This is my first main layout when I am clicking, if I want to save data so I will click in this button to save and suppose I want to check data so I will, I can click this check data button. In check data you can see that there are some text that is name, telephone, Skype ID addresses means these are the fields I have in my database. I have created that those fields into my database and I am displaying the data from the database to using this list view. This is for your check data, in this you can get your, you can retrieve your data in this layout. To save data here you can see I have used four edit text for the four fields and one button to save that. If I am clicking on back it will directly jump into your main dot XML. So now we will access this database, how we can access, sorry how we can access the widgets of this layouts. For that I have created four files for one for one dot check dot data dot Java is used to check data from your database to retrieve data from your database and this file is to save data to your database and this data manipulator will handle the check data and the save data operations. And this is the main, your main activity, so first we will see this main activity here you can see I have access first two button that is your save button and check button. I have access this two button and this you can see for access by find by ID and I have used, I have set on click listener to those buttons and then when I am clicking, clicking on button one, so the one intent will fire and it will show you the save data class. So save data class is your, this file will invoke and after what this file is doing, this file is going to save the data. Again in this file, this file is directly accessing your save dot XML file and using that and what are the widgets you have used in your save dot XML file, you will access by using, you have access by using find by ID and this and after there is two button one is for home, if there is a back button if you are clicking on back button it will directly jump on to your home and then if you are clicking on save button then it will save that your information. Before saving it will ask you for the next, if you want to, if you save one information then it will and after that you will click on save button then it will ask you save, if you want to save next information, so you can directly save there. We will see that how that particular dialogues is implemented. Now you can see here when I am clicking button zero that is home, so one intent will fire and directly database sample dot java file will call, that is your this layout and then if I am clicking on add button that is second save button, so all those at first have access those edit text and I am copying the content of those edit text into a string. You can see this and I have called this data manipulator to save that values and after that I have used insert method to save those values into your database and then I have created one dialogue, one if I have stored one information then again it will ask you to save another, so if I want to save then I will click yes, if I do not then I will click no and if I am clicking no it will directly jump into your home page, means this is your main XML file and next is your check data, here I am retrieving that data and I am displaying that data into this list, again here you can see I have used data manipulator, so we will run this program we will see the output, you can see this there is a database sample that there are two button, if I am clicking save button, so it will call this edit text, sorry your save dot XML, in this we have to enter the name, then I will enter my telephone number something like this, then I am using Skype ID, so something dot and my address, I am writing anything here or for addressing myself, so I will use IIT Bombay and when I will click on save button it will ask me one information, information save successfully when this information is saved, add another data info if you want to add another data you can click yes, so you can again get the same activity, you will be the same activity, so again you can delete this and you can enter another data, so I can write here tcsc any and I am saving this, again it will ask me I do not want to save next data, so I will click no, so it will directly jump on to your first main dot XML, so to check those data is saved or not we will go there to we will click this check data button, so we will click on this check data, now you can see two data is displaying here from your database, again this is if you want to erase your this value, this data from your database then you have to go to first find your application that is data sample, database sample, this is your application hold this go here app info and click on the clear data, so your data will be clear from here, so now first we will check our data from the data how we can see our database we have to go to d d m s, inside the d d m s again you have to go to data and data, see that package of your application that is com.app database sample, find that com.app this is my package, this is my database, you can see there is my database db is created, if you want to see the value of those content from this database you have to pull this into your desktop, so again you can see that last time when I am pulling some files or from here to the desktop, so it is showing me this fail to pull selection, because there are some we have to give some permission to pull that information from device to desktop, but anyway we got that whatever the value we have saved here, whatever the value we have saved in this database we have displayed that value. If you want to clear your database then you have to follow that clear your data, hold this app info then clear your data, go back to your application, open, check your data there is no data, means you have cleared your data from your database, so this is the way we can use storage options on our android application.