javaandroidstorageopenstreetmaposmdroid

OSMDroid - Map does not display


I am trying to load open street maps on my app.

My code is the following:

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

import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.views.MapView;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;


public class OsmActivity extends Activity {
private MapView         mMapView;
private MapController   mMapController;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_osm);


    Configuration.getInstance().setUserAgentValue(getPackageName());

    mMapView = (MapView) findViewById(R.id.map);
    mMapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
    mMapView.setBuiltInZoomControls(true);
    mMapController = (MapController) mMapView.getController();
    mMapController.setZoom(13);
    GeoPoint gPt = new GeoPoint(51500000, -150000);
    mMapController.setCenter(gPt);
}
}

I am using osmdroid 5.6.5 (via Maven).

I define my user agent according to the osmdroid doc.

Configuration.getInstance().setUserAgentValue(getPackageName());

I have these API settings:

android {
compileSdkVersion 26
defaultConfig {
    applicationId "com.loco_zero.locolizetestsosm"
    minSdkVersion 21
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

With these permissions:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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.READ_EXTERNAL_STORAGE" />

I have the grid and the zoom buttons, but the map won't show. I get these errors:

I/StorageUtils: /sdcard is NOT writable
I/StorageUtils: /storage/emulated/0 is NOT writable
I/OsmDroid: Using tile source: Mapnik
E/SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
E/SQLiteLog: (14) os_unix.c:31278: (13) open(/storage/emulated/0/osmdroid/tiles/cache.db) - 
E/SQLiteDatabase: Failed to open database '/storage/emulated/0/osmdroid/tiles/cache.db'.
              android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

D/OsmDroid: Unable to store cached tile from Mapnik /13/4090/2719, database not available.

What am I doing wrong? Can I tell OSMDroid to use something other than the sdcard folder?

EDIT

I added the following to get the permissions at runtime, it did the trick:

    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG, "Permission is granted");
        } else {
            Log.v(TAG, "Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }

Solution

  • Make sure you are enabling the runtime permissions correctly.. You will need external storage as well as the internet ones. Also check your manifest permissions