Post Top Ad

Post Top Ad

Sunday 30 April 2017

#File System in Android Apps.


Android has a disk-based file system. There are two storage areas: internal and external. Internal storage refers to the built-in, non-volatile memory. Devices also can come with removable memory (like a SD card), which is referred to as external storage.The internal storage is also occasionally referred to as permanent storage. Files saved in internal storage are accessible only to your app by default.

External storage can be removed at any time and files saved here can be accessible to everyone.
    Post a comment
    Email Article
    Print Article
    Share Articles
Applications can allow themselves to be installed on the external storage by specifying the android:installLocation attribute. (The default is internal storage.)

When the user uninstalls the application, if the app is installed on internal storage, all files are removed. On the other hand, if the application was installed on external storage, the app files are moved only if we save them in the directory that is obtained from calling API getExternalFilesDir().

To grant everyone permission to your files, you need to declare the following in your manifest file.

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

To save files on internal storage, we need to acquire a directory as a file by calling either the getFilesDir() API (which represents an internal directory for your app), or by calling getCacheDir(), which represents your app's temp cache files.

A new file can be created by specifying the file and the name of the directory where the file needs to be created by using the File constructor as follows.

File file = new File(context.getFilesDir(),
   fileName);
Hands On

Let us create a simple application that demonstrates working with files.
Android Studio and Start a new Android Studio Project.

File1
Figure 1: Starting a new Android Studio Project

Provide FileDemo as the Application Name and Click Next.

File2
Figure 2: Configuring the new project

On the next screen, leave the default values and click Next.

File3
Figure 3: Leaving the default values in place

On the "Add an activity to Mobile" page, choose "Blank Activity". This creates an application with a single activity.

File4
Figure 4: Adding a new, blank activity

We are then prompted to customize the activity. We will leave the default values unchanged.

File5
Figure 5: Here is where you would customize the activity (but don't do so)

Click Finish to creating the project files.

Next, Open the AndroidManifest.xml file.

File6
Figure 6: The AndroidManifest.xml file

We need to declare permissions to write to files here.

<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/
      res/android"
   package="com.example.vipul.filedemo">

   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme">
         <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
               <intent-filter>
                  <actionandroid:name=
                     "android.intent.action.MAIN"/>
                  <categoryandroid:name=
                     "android.intent.category.LAUNCHER"/>
               </intent-filter>
         </activity>
   </application>
   <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>

We are only doing the preceding declaration to demo how to declare what is needed to write to external storage. We now will switch to working with a file on internal storage.

Switch to the design view and add two buttons as shown in the following code.

  1. <RelativeLayoutxmlns:android="http://schemas.android.com/
  2.       apk/res/android"
  3.    xmlns:tools="http://schemas.android.com/tools
  4.       "android:layout_width="match_parent"
  5.    android:paddingRight="@dimen/activity_horizontal_margin"
  6.    android:paddingTop="@dimen/activity_vertical_margin"
  7.    android:paddingBottom="@dimen/activity_vertical_margin
  8.       "tools:context=".MainActivity">
  9.    <TextViewandroid:text="@string/hello_world"
  10.       android:layout_width="wrap_content"
  11.       android:layout_height="wrap_content"
  12.       android:id="@+id/textView"/>
  13.       <Button
  14.          android:layout_width="wrap_content"
  15.          android:layout_height="wrap_content"
  16.          android:text="CreateFile"
  17.          android:id="@+id/buttonCreateFile"
  18.          android:layout_below="@+id/textView"
  19.          android:layout_alignLeft="@+id/buttonCreateTempFile"
  20.          android:layout_alignStart="@+id/buttonCreateTempFile"
  21.          android:clickable="true"/>
  22.       <Button
  23.          android:layout_width="wrap_content"
  24.          android:layout_height="wrap_content"
  25.          android:text="CreateTempFile"
  26.          android:id="@+id/buttonCreateTempFile"
  27.          android:layout_below="@+id/buttonCreateFile"
  28.          android:layout_toRightOf="@+id/textView"
  29.          android:layout_toEndOf="@+id/textView"
  30.          android:layout_marginTop="41dp"/>
  31. </RelativeLayout>

Next, we will wire up the click event handlers to create files.

Create two method stubs in MainActivity.java

publicvoidsendFileMessage(Viewview){
   //Dosomethinginresponsetobuttonclick
}

publicvoidsendCacheFileMessage(Viewview){
   //Dosomethinginresponsetobuttonclick
}


Next, we will wire up the click events in the layout file.

  1. <Button
  2.    android:layout_width="wrap_content"
  3.    android:layout_height="wrap_content"
  4.    android:text="CreateFile"
  5.    android:id="@+id/buttonCreateFile"
  6.    android:layout_below="@+id/textView"
  7.    android:layout_alignLeft="@+id/buttonCreateTempFile"
  8.    android:layout_alignStart="@+id/buttonCreateTempFile"
  9.    android:clickable="true"
  10.    android:onClick="sendFileMessage"/>
  11. <Button
  12.    android:layout_width="wrap_content"
  13.    android:layout_height="wrap_content"
  14.    android:text="CreateTempFile"
  15.    android:id="@+id/buttonCreateTempFile"
  16.    android:layout_below="@+id/buttonCreateFile"
  17.    android:layout_toRightOf="@+id/textView"
  18.    android:layout_toEndOf="@+id/textView"
  19.    android:layout_marginTop="41dp"
  20.    android:onClick="sendCacheFileMessage"/>
  21.  
  22.  
  23. Now, we implement the method stubs to create the files.
  24. public void sendCacheFileMessage(View view) {
  25.    // Do something in response to button click
  26.    File file = new File(view.getContext().getCacheDir(),
  27.       "cachefileName");
  28.    tView = (TextView) findViewById(R.id.textViewStatus);
  29.    tView.setText("cache file create");
  30.    }
  31.    @Override
  32.    public boolean onCreateOptionsMenu(Menu menu) {
  33.       // Inflate the menu; this adds items to the
  34.       // action bar if it is present.
  35.       getMenuInflater().inflate(R.menu.menu_main, menu);
  36.       return true;
  37. }

No comments:

Post a Comment