Category: Android

How to create and use Offline Maps with an Android App?

When the app we know that the app we have proposed to make is going to be used in areas with no wifi or cellular networks nearby or when we want to offer an offline map option we make use of Offline maps. Otherwise it would be easier to implement the Android Google Map API.

Maps are made up of a collection of images often referred to as tiles. There are sets of tiles for each zoom level. So the first step in creating an offline map is selecting the area and zoom level to create the tiles.

Creating the Offline Map Tiles

We make use of the Mobile Atlas Creator.

1. Download and extract the files

2. Run the application with the .exe or the .sh script. Make sure that suitable permissions (execute) is given to the file.

3. Use the right click to navigate around the map. Use the right mouse button to select an area.

4. Make sure the MapSource is set to OpenStreetMap MapQuest. You can try others but the one that works for me is OpenStreetMap MapQuest.

5. Make sure the Atlas Format is Osmdroid ZIP.

6. Select the Zoom Levels less will make the file size smaller. Select Recreate/adjust map tiles, PNG and leave the size as deafult. Note: It has to be PNG, JPEGs won't work on Android

7. Name the selection and click Add Selection.

8. Then Create Atlas

Adding the Offline Map Tiles to your Android Project

The Map tiles folder or .zip needs to be named specifically and placed in the correct location.

1. Rename the zip to

2. Rename the folder in the zip to MapquestOSM. Take note of the spelling and case.

3. Place the file into app/src/main/assets

Copying the Map Files to the SD Card

We need to copy the Map Tiles to the SD Card (External Storage) on the Android Device.

1. Add a function preferably in the application class to make sure the file is copied across to the device.

public static void copyTilestoSDCard(){
//save zip to sd
AssetManager am = BaseApp.getmContext().getAssets();
InputStream is;
//zip file in assets root
String sourceFileName = "";
//zip file in SD card
String destinationFileName = "";
String osmDirName = "osmdroid";

File osmDir = new File(Environment.getExternalStorageDirectory() + File.separator + osmDirName);

if (!osmDir.exists()){
String filePath = Environment.getExternalStorageDirectory() + File.separator +
osmDirName + File.separator + destinationFileName;
File destFile = new File(filePath);
if (!destFile.exists()){
//file doesn't exist
try {
is =;
FileOutputStream fo = new FileOutputStream(filePath);

byte[] b = new byte[1024];
int length;
while ((length = != -1){
fo.write(b, 0, length);
} catch (IOException e) {

Setting Required Permissions for the Offline Map

 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Add Open Street Maps Droid with Gradle

You need the OSMDroid, you can add to gradle with:

compile 'org.osmdroid:osmdroid-android:4.2'
compile 'org.slf4j:slf4j-simple:1.6.1'

Now to Display the Offline Map on the Android Device

To display in the app make sure your layout file contains:


The controller Fragment or Activity should then intialise the map:

org.osmdroid.views.MapView mOfflineMapView = findById(;
//Make sure you don't get the map from online
//Set the Tile Source (That is MapqueueOSM)

Then set all your required settings for the MapView:


And there we go.


Using an Existing SQLite Database with an Android Project

This article highlights how to use an Existing SQLite database with an Android Project.


A sqlite3 database is required as it is the only db that android supports (probably because it is a single simple file). Converting between databases can be tricky sometimes. In this case I needed to convert a MSSQL database to SQLite so I made use of the following repos:

Important Note Regarding ORM's

Now you need to make a choice. Will you a using an ORM, an Object Relational Mapper. An ORM is a package that converts database records into objects. A popular approach is active record.

Some Android ORM's:

Now if you have chosen to use an ORM take note of the following.

  • It will create the database for you based on your models and hence can't use existing data
  • It will create tables exactly based on your models
Note: If the above is not true please comment

So basically use of an ORM is not applicable in this case as we want to ship the app with existing data.

Where is the Database Stored

The database is stored in /data/data/

So the database has to be copied over from the app to that location. We do this using the following package: Android SQLite Asset Helper. I am unaware of the ability to do this with the native Android SDK.

Where should I put the Existing SQLite file in the Android Project Structure

You need to place the database in: Project/app/src/main/assets.databases/mydb.db

Shipping your App with an existing SQLite database

First we need to add the dependency in build.gradle

dependencies {
    compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'

So do the steps above. Now we need to create a java class that uses the SQLiteAssetHelper:

public class MyDb extends SQLiteAssetHelper{
  private static final String DB_NAME = "mydb.db";
  private static final int DB_VERSION = 1;

  public MyDB(Context context){
    super(context, DB_NAME, null, DB_VERSION);

Awesome. Now to do the initial copy across. This is done when we first call getReadableDatabase().

So in or wherever you want to do this.

MyDb db = new MyDb(this);

Querying - Getting Data with a Cursor

Now to Query data and get a Cursor (not an Object) add a function to

public Cursor getData(){
      SQLiteDatabase db = getReadableDatabase();
        Cursor mCur = db.query(
                TABLE, //table
                //We Need a _id (It's required for CursorAdapter)
                null, //where clause
                null, //where parameters
                null, //groupby
                null, //having
                COLUMN_NAME + " ASC"
        return mCur;

Now for getting the data:

Cursor myCursor = db.getTrees();

Alright, so that is that. It's not the best solution so any comments please discus below.

Root and Backup your Android Device on Ubuntu

How to Backup your Android Device on Linux


  • Android SDK Installed
  • ADB is accessible in path
  • USB cable connected to device

adb backup [-f ] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] [-system|-nosystem] []
                               - write an archive of the device's data to .
                                 If no -f option is supplied then the data is written
                                 to "backup.ab" in the current directory.
                                 (-apk|-noapk enable/disable backup of the .apks themselves
                                    in the archive; the default is noapk.)
                                 (-obb|-noobb enable/disable backup of any installed apk expansion
                                    (aka .obb) files associated with each application; the default
                                    is noobb.)
                                 (-shared|-noshared enable/disable backup of the device's
                                    shared storage / SD card contents; the default is noshared.)
                                 (-all means to back up all installed applications)
                                 (-system|-nosystem toggles whether -all automatically includes
                                    system applications; the default is to include system apps)
                                 ( is the list of applications to be backed up.  If
                                    the -all or -shared flags are passed, then the package
                                    list is optional.  Applications explicitly given on the
                                    command line will be included even if -nosystem would
                                    ordinarily cause them to be omitted.)

I used:

adb backup -f backup.ab -apk -obb -shared -all

You will need to accespt it on the device