user3070214
user3070214

Reputation: 17

osmdroid error loading tile(on mobile like Samsung S5 )

I am trying to show a offline map with a Polyline on it using a OSMdroid library. Everything are going well , no mater what the version of the android mobile.

However when i run the code on Samsung s5 or LG g4 the code stop running and the offline map tiles not load , load only the half. The exception that i get is:OutofmemoryError loading bitmap , Error loading tile , Low memory exception etc. Please help me to solve this problem. I am sorry for my English ! Thank you! My code:

try{

    NameofJsonFile = ((MainFragmentActivityRoutes)getActivity()).getNameofJsonFile();



//android:layerType=""

            mMapView = (MapView) v.findViewById(R.id.mapview2);
            mMapView.setTileSource(TileSourceFactory.MAPNIK);
            mMapView.setBuiltInZoomControls(true);
            mMapView.setUseDataConnection(false);

            mMapView.setMinZoomLevel(minZoom);
            mMapView.setMaxZoomLevel(maxZoom);
            mMapController = (MapController) mMapView.getController();
            mMapController.setZoom(minZoom);


        if(cordinates==null){
              Log.e("", "edooooooo!!!");
              cordinates=  new myasinc().execute(NameofJsonFile).get();   

        }  


        //almopia prosoxi edo o xartis einai etoimos    
        GeoPoint locationStart = new GeoPoint((double) ((ArrayList<?>)cordinates.getFeatures().get(0).getGeometry().getCoordinates().get(0)).get(1), (double)((ArrayList<?>)cordinates.getFeatures().get(0).getGeometry().getCoordinates().get(0)).get(0));
        GeoPoint locationEnd = new GeoPoint(new GeoPoint((double) ((ArrayList<?>)cordinates.getFeatures().get(0).getGeometry().getCoordinates().get(cordinates.getFeatures().get(0).getGeometry().getCoordinates().size()-1)).get(1), (double)((ArrayList<?>)cordinates.getFeatures().get(0).getGeometry().getCoordinates().get(cordinates.getFeatures().get(0).getGeometry().getCoordinates().size()-1)).get(0)));
        //

        mMapController.setCenter(locationStart);
        mMapController.animateTo(locationStart);
        anotherOverlayItemArray = new ArrayList<OverlayItem>();

        OverlayItem item = new OverlayItem("0, 0", "0, 0",locationStart);
        sample = ResourcesCompat.getDrawable(getResources(), R.drawable.start, null);
        marker = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(((BitmapDrawable) sample).getBitmap(), 60, 60, true));
        item.setMarker(marker);
        anotherOverlayItemArray.add(item);


         item = new OverlayItem("0, 0", "0, 0",locationEnd);
         sample = ResourcesCompat.getDrawable(getResources(), R.drawable.end, null);
         marker = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(((BitmapDrawable) sample).getBitmap(), 60, 60, true));
         item.setMarker(marker);
         anotherOverlayItemArray.add(item);


       resourceProxy = new DefaultResourceProxyImpl(getActivity());
       anotherItemizedIconOverlay= new ItemizedIconOverlay<OverlayItem>(anotherOverlayItemArray, marker, null, resourceProxy);
       mMapView.getOverlays().add(anotherItemizedIconOverlay);


       Polyline line = new Polyline(getActivity());
       line.setSubDescription(Polyline.class.getCanonicalName());
       line.setWidth(2f);
       ArrayList<GeoPoint> pts = new ArrayList<>();
       //here, we create a polygon, note that you need 5 points in order to make a closed polygon (rectangle)

       mMapView.getOverlayManager().add(line);

             for(int i=0; i<cordinates.getFeatures().get(0).getGeometry().getCoordinates().size(); i++){

                    if(i%2==0){
                        pts.add(new GeoPoint((double) ((ArrayList<?>)cordinates.getFeatures().get(0).getGeometry().getCoordinates().get(i)).get(1), (double)((ArrayList<?>)cordinates.getFeatures().get(0).getGeometry().getCoordinates().get(i)).get(0)));

                    }

              }


    line.setPoints(pts);
    line.setGeodesic(true);

      Iterator<Overlay> iterator = mMapView.getOverlays().iterator();
    while(iterator.hasNext()){
        Overlay next = iterator.next();
        if (next instanceof TilesOverlay){
            TilesOverlay x = (TilesOverlay)next;
            x.setOvershootTileCache(x.getOvershootTileCache() * 2);
            Toast.makeText(getActivity(), "Tiles overlay cache set to " + x.getOvershootTileCache(), Toast.LENGTH_LONG).show();
            break;
        }
    }

} catch(Exception e){

}

Upvotes: 0

Views: 789

Answers (1)

k3b
k3b

Reputation: 14755

On my android device osmdroid caches the loaded tiles in /storage/sdcard0/osmdroid/tiles..... .

If there is not enought space left on /storage/sdcard0/ then you get "Low memory exception"

you have to remove files from "/storage/sdcard0/" ...

Upvotes: 0

Related Questions