How to create android app with movable to sd card feature?

In this post, I will be discussing how to create Android application that comes with a feature to move it to SD card.

Most Android applications enable the ability for users to choose the application install location (Internal memory or SD card). However, for some applications, this feature doesn’t make as much sense. For example, if you have an app widget, live wallpaper, or other sort of service or utility application (such as a file manager), your application won’t be of any use if the SD card is removed.

Although users may not remove their SD cards frequently, the SD card is unmounted when it’s mounted via USB for use on a computer. This will potentially increase the frequency that the user will have to reconfigure apps that are affected. For a full list of apps that are affected when removed, see this Google Android reference.

How to enable move to SD card feature?

It is very simple to enable move to SD card feature in Android applications, all you need to do is specify the installLocation attribute in Android manifest as ‘auto’.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.prgguru.sdmovable"
    android:versionCode="1"
    android:versionName="1.0" android:installLocation="auto">
...
</manifest>

installLocation attribute can be assigned with following values:

auto

It indicates that your application may be installed on the internal memory (or) external storage, but you don’t have a preference of install location. The system will decide where to install your application based on several factors. The user can also move your application between the two locations.

internalOnly

If you’re certain that your application should never be installed on the external storage, assign installLocation attribute value as ‘internalOnly’.

preferExternal

You request that your application be installed on the external storage, but the system does not guarantee that your application will be installed on the external storage. If the external storage is full, the system will install it on the internal storage. The user can also move your application between the two locations.

Let me demonstrate it with simple Android application.

Here are the steps to create Android application with ‘Movable to SD card’ feature.

Step 1:
  • Create new android project [File >> New >> Android Application Project] with project name SDMovableApp
  • Enter package name as ‘com.prgguru.sdmovable’
  • Choose Minimum Required SDK, Target SDK and Compile with as shown in the below screen=print. If you don’t have latest SDK installed, they will not be populated in the dropdownlist, I would recommend you to update Android SDK manager with latest SDKs. Confused on choosing these options? Take a look at Minimum Required SDK – Target SDK – Compile With post.
  • Click Next button and fill other particulars to create the project.

SDMovableApp_Project_Creation

Step 2

Open activity_main.xml present under SDMovableApp/res/layout and replace it with below code. This layout has only one button at the center of the activity which will open ‘App Info’ activity when it is clicked.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- Button which will open App Info activity when it is clicked  -->
    <Button android:id="@+id/btnFindMe" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" android:text="App Settings" android:onClick="openAppInfo">
    </Button>

</RelativeLayout>
Step 3

Open MainActivity.java from com.prgguru.sdmovable package and replace it with below code. Each line of code is explained with comment, if you still have any doubt raise the question right from comment box.

MainActivity.java

package com.prgguru.sdmovable;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Layout XML
		setContentView(R.layout.activity_main);
	}
	/**
	 * Method fired when button App settings button is clicked
	 * 
	 * @param view
	 */
	public void openAppInfo(View view) {
		// This application's package name
		String packageName = "com.prgguru.sdmovable";

		try {
			// Create Intent object to open the App info Activity of this application
			Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
			// Set this application package as Data
			intent.setData(Uri.parse("package:" + packageName));
			// Open App info Activity
			startActivity(intent);
		} 
		// If Application package doesn't exist, open default Apps info Activity
		catch (ActivityNotFoundException e) {
			// Open the generic Apps Activity:
			Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
			startActivity(intent);
		}
	}
}
Step 4

Specify the installLocation attribute in AndroidManifest as ‘auto’.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.prgguru.sdmovable"
    android:versionCode="1"
    android:versionName="1.0" android:installLocation="auto">

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.prgguru.sdmovable.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

[pglinkadssmall1]

Demo

That’s all. It’s time to test our code.

Run the application using emulator or device by right clicking on the project >> Run as >> Android applicaiton >> Choose emulator or device.

When you set installLocation as ‘auto’, App Info screens will look similar to below screen-prints:


When you set installLocation as ‘internalOnly’, App Info screens will look similar to below screen-prints:


When you set installLocation as ‘preferExternal’, App Info screens will look similar to below screen-prints:



Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android  is the installation file similar to exe in windows.

[pglinkadssmall]

If you feel this article is helpful and interesting please spread a word about it to your friends and colleagues by sharing the article in Facebook or Twitter.


Share

You are always welcome to provide your comments and feedback from comment box.

[pgwriteforus]
[pgfeedback]

Author: Android Guru

Share This Post On
Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares