Android Internal and External Storage difference

This post assumes that you have understanding of standard file input/output APIs in java.io.
When working with files in Android, first you need to be aware of Internal and External File storage.

Android devices have two file storage areas: “internal” and “external” storage. During initial days, internal storage means the memory which is built into device, and external storage means external devices such as SD Card which gets mounted to Android device. But these days, even the built in memory has internal and external storage as well. Below are differences between Internal and external Storage

Internal storage:
It’s always available.
Files saved here are accessible by only your app by default.
When the user uninstalls your app, the system removes all your app’s files from internal storage.
Internal storage is best when you want to be sure that neither the user nor other apps can access your files.

External storage:
It’s not always available, because the user can mount the external storage as USB storage and in some cases remove it from the device.
It’s world-readable, so files saved here may be read outside of your control.
When the user uninstalls your app, the system removes your app’s files from here only if you save them in the directory from getExternalFilesDir().
External storage is the best place for files that don’t require access restrictions and for files that you want to share with other apps or allow the user to access with a computer.


import android.app.Activity;
import android.os.Bundle;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import java.io.*;

public class MyActivity extends Activity {

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

        writeToFile(getApplicationContext(), "aaaaaaaaaaaa bbbbbbbbbbb kjuhiuh");

        String file_content = readFromFile();

        System.out.println("Content of file is :"+file_content);

        Log.i("ccccccccccContent of file is :", file_content);

    }

    private void writeToFile(Context ctx,String data) {
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(ctx.openFileOutput("config.txt", Context.MODE_PRIVATE));
            outputStreamWriter.write(data);
            outputStreamWriter.close();
        }
        catch (IOException e) {
            Log.e("Exception", "File write failed: " + e.toString());
        }
    }

    private String readFromFile() {

        String ret = "";

        try {
            InputStream inputStream = openFileInput("config.txt");

            if ( inputStream != null ) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receiveString = "";
                StringBuilder stringBuilder = new StringBuilder();

                while ( (receiveString = bufferedReader.readLine()) != null ) {
                    stringBuilder.append(receiveString);
                }

                inputStream.close();
                ret = stringBuilder.toString();
            }
        }
        catch (FileNotFoundException e) {
            Log.e("login activity", "File not found: " + e.toString());
        } catch (IOException e) {
            Log.e("login activity", "Can not read file: " + e.toString());
        }

        return ret;
    }

}

Download Android File Example Source Code

Android Photo Album Application

Below is an example, on how to develop a photo Album on Android

Activity file


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    int count = 1;
    FrameLayout frame;
    ImageView imageview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        frame = (FrameLayout) findViewById(R.id.framelayout);
    }

    public void btn2click(View v) {

        imageview = (ImageView) frame.findViewWithTag(String.valueOf(count));
        imageview.setVisibility(View.INVISIBLE);
        count++;
        if (count > 6)
            count = 1;

        // Show next image
        imageview = (ImageView) frame.findViewWithTag(String.valueOf(count));
        imageview.setVisibility(View.VISIBLE);
    }

    public void btn1click(View v) {
        imageview = (ImageView) frame.findViewWithTag(String.valueOf(count));
        imageview.setVisibility(View.INVISIBLE);

        count--;
        if (count < 1)
            count = 6;

        imageview = (ImageView) frame.findViewWithTag(String.valueOf(count));
        imageview.setVisibility(View.VISIBLE);

    }
}

XML Layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.bikash.imageviewexcelgyan.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/framelayout">

        <ImageView
            android:src="@drawable/image"
            android:layout_width="match_parent"
            android:layout_height="373dp"
            android:id="@+id/imageView"
            android:layout_gravity="center_horizontal|top"
            android:tag="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next"
            android:id="@+id/btn2"
            android:layout_gravity="right|bottom"
            android:onClick="btn2click" />

        <ImageView
            android:src="@drawable/img"
            android:layout_width="match_parent"
            android:layout_height="373dp"
            android:id="@+id/imageView2"
            android:layout_gravity="center_horizontal|top"
            android:tag="2"
            android:visibility="invisible"/>

        <ImageView
            android:src="@drawable/imagec"
            android:layout_width="match_parent"
            android:layout_height="373dp"
            android:id="@+id/imageView3"
            android:layout_gravity="center_horizontal|top"
            android:tag="3"
            android:visibility="invisible"/>

        <ImageView
            android:src="@drawable/imaged"
            android:layout_width="match_parent"
            android:layout_height="373dp"
            android:id="@+id/imageView4"
            android:layout_gravity="center_horizontal|top"
            android:tag="4"
            android:visibility="invisible"/>

        <ImageView
            android:src="@drawable/imagee"
            android:layout_width="match_parent"
            android:layout_height="373dp"
            android:id="@+id/imageView5"
            android:layout_gravity="center_horizontal|top"
            android:tag="5"
            android:visibility="invisible"/>

        <ImageView
            android:src="@drawable/imagef"
            android:layout_width="fill_parent"
            android:layout_height="373dp"
            android:id="@+id/imageView6"
            android:layout_gravity="center_horizontal|top"
            android:tag="6"
            android:visibility="invisible"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Previous"
            android:id="@+id/btn1"
            android:layout_gravity="left|bottom"
            android:onClick="btn1click"/>

    </FrameLayout>
</RelativeLayout>

ImageViewAlbum
Android Photo Album App Download(on Android Studio)

Below are the screen shots, of the app

Android Simple Photo Album
Android Photo Album

Android Photo Album
Android Photo Album

You may also like to read:
permission in androidmanifest file
calculator app in android
Android difference between px and sp

This App is contributed by, Bikash Paul

Calculator example in Android

Below is an example on how to develop Calculator example on Android platform.
android-calculator-example

This Application is used to perform arithmetic operations on two integers, which need to be entered in the first two EditText views.
Select the operation you want to perform.
Result is displayed in the last EditText.

Below is screen shot of the App.

XML Layout file main.xml

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

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/firsNum"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/secondNum"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/add"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="89dp"
android:layout_height="wrap_content"
android:text="Sub"
android:id="@+id/sub"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:text="Div"
android:id="@+id/div"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="91dp"
android:layout_height="wrap_content"
android:text="Mul"
android:id="@+id/mul"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/result"/>
</LinearLayout>

Below is MyActivity.java file

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MyActivity extends Activity implements View.OnClickListener{

    private Button add;
    private Button sub;
    private Button mul;
    private Button div;
    private EditText firstNum;
    private EditText secondNum;
    private EditText results;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        add=(Button)findViewById(R.id.add);
        sub=(Button)findViewById(R.id.sub);
        mul=(Button)findViewById(R.id.mul);
        div=(Button)findViewById(R.id.div);
        firstNum=(EditText)findViewById(R.id.firsNum);
        secondNum=(EditText)findViewById(R.id.secondNum);
        results=(EditText)findViewById(R.id.result);
        add.setOnClickListener(this);
        sub.setOnClickListener(this);
        mul.setOnClickListener(this);
        div.setOnClickListener(this);
    }
    public void onClick(View v){
        int num1=Integer.parseInt(firstNum.getText().toString());
        int num2=Integer.parseInt(secondNum.getText().toString());
        if(v.getId() == R.id.add){
         int result=num1+num2;
            results.setText(String.valueOf(result));
        }else if(v.getId() == R.id.sub){
            int result=num1-num2;
            results.setText(String.valueOf(result));
        }else if(v.getId() == R.id.mul){
            int result=num1*num2;
            results.setText(String.valueOf(result));
        }else if(v.getId() == R.id.div){
            int result=num1/num2;
            results.setText(String.valueOf(result));
        }
    }
}

You may also like to read:
In Android, what is px,dp,dip and sp?
In Android, what is difference between fill_parent and match_parent

Android Difference between difference between px,dp,dip and sp

Since Android platform can be used to develop apps for various devices with different screen sizes. Various units have been provided to specify sizes like font, width, height,etc… Below is description on the same.
px
Pixels – corresponds to actual pixels on the screen.
in
Inches – based on the physical size of the screen.
1 Inch = 2.54 centimeters
mm
Millimeters – based on the physical size of the screen.
pt
Points – 1/72 of an inch based on the physical size of the screen.
dp or dip
Density-independent Pixels – an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both “dip” and “dp”, though “dp” is more consistent with “sp”.
sp
Scale-independent Pixels – this is like the dp unit, but it is also scaled by the user’s font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user’s preference.

In Android, Difference between fill_parent, match_parent

Below values are often used with android:layout_width and android:layout_height in XML Layout files

1. fill_parent equivalent integer value is -1. Specifies that view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent.
2. match_parent equivalent integer value is -1. Specifies that view should be as big as its parent (minus padding). Introduced in API Level 8. This has been introduced in API Level 8, to avoid confusion with the term fill_parent, though the behavior is exactly same.
3. wrap_content equivalent integer value is -2. Specifies that view should be only big enough to enclose its content (plus padding).