0

I've this snippet of code which I would like to optimize it. I've a method which is been called regularly by the OSMdroid library to load tons of maptiles. This method directly invokes the filestream and load the bitmap directly and will return the bitmap once loaded on the main UI thread.

Although I've managed to run in the background using AsyncTask with parallel executor. Sometimes with plenty number of overlays (itemized) in the mapview this snippet of code runs slower as GC_FO_ALLOC is triggered regularly for allocation, and in my log messages I get Grow Heap (frag case). I tried many ways to work around, but not effective enough. For some reason this task is being executed on main thread is my feeling as in my log messages I also get Skipped xx frames, the application may be doing lot of task. Any idea how can this be made better? The thing is method has to return back, as soon as it loads, there by how can I allow this method to wait until mapview is not panned or zoomed, then load the tiles?

@SuppressWarnings("deprecation")
    @Override
    public Drawable getDrawable(final InputStream aFileInputStream) throws LowMemoryException {

        try {
            df = new DisplayFile();
            df.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, aFileInputStream);
            return new BitmapDrawable(df.get());
        } catch (final OutOfMemoryError e) {
            System.gc();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

private class DisplayFile extends AsyncTask<InputStream, Bitmap, Bitmap> {

        InputStream path;

        @Override
        protected Bitmap doInBackground(InputStream... arg0) {
            path = arg0[0];
            BitmapFactory.Options mBitOpt = new BitmapFactory.Options();
            mBitOpt.inDither = false;
            mBitOpt.inSampleSize = 1;
            mBitOpt.inPurgeable = true;
            mBitOpt.inInputShareable = true;
            mBitOpt.inPreferredConfig = Bitmap.Config.ARGB_8888;
            final Bitmap mBitmap = BitmapFactory.decodeStream(path,null,mBitOpt);
            return mBitmap;
        }
    }
zIronManBox
  • 4,967
  • 6
  • 19
  • 35

1 Answers1

0

As far as I understand, you are trying to display an offline map using osmdroid.

Android is unable to load "tons of map tiles" in memory, because it just doesn't provide enough memory heap to each application.

This is why osmdroid has advanced mechanisms (background loading, LRU cache, ...)

Why are you not just using these standard osmdroid mechanisms?

Look at this post for more details about off-line maps on osmdroid: Download maps for osmdroid

Community
  • 1
  • 1
MKer
  • 3,430
  • 1
  • 13
  • 18
  • My mapview works perfectly when no overlays are there, GC does trigger to allocate memory at times, but its done pretty fast and maptiles are loaded normally and swiftly. – zIronManBox Jun 19 '14 at 11:31
  • Once more question to what you have told, `osmdroid has advanced mechanisms (background loading, LRU cache, ...)` I did tried asking how could I cache using `MapTileLRUcache`, but then I understood then `OSMdroid` internally uses it, so it won't make much sense to use again. Although I would want to know or any example of how to load in the background? I'm currently using `AsyncTask` to load tiles to make it run forcefully in the background, how can I optimize the above shown code? – zIronManBox Jun 19 '14 at 11:33
  • And I did see your offline maps tutorial, nice. But I need to be extremely flexible as mine is similar pattern with custom tiles. Hence i'm inheriting `XYTileSource` to create my own class. – zIronManBox Jun 19 '14 at 11:40
  • Correct me if I'm wrong: when you don't have "plenty of overlays", you have no issue with your customized XYTileSource: your mapview is working perfectly, map tiles are loaded normally, panning and zooming are fast. Issues (lot of full GC, slow display) appear only when you have "plenty of overlays". Is it a correct description? – MKer Jun 20 '14 at 11:47
  • Yes , that's sums it up! And when I clear all the overlays also, the mapview recovers and works fine. – zIronManBox Jun 21 '14 at 08:14
  • OK. So your issue is related to the fact that you have "plenty of overlays". Trying to solve it by optimizing tiles loading is completely irrelevant. Now if you raise a question describing as clearly as possible your overlays issue, maybe somebody will be able to help => how many, of which type; and for polygons/polylines: average number of points. – MKer Jun 21 '14 at 11:44
  • As of now When overlays are visible, I clear the tilecache every time the user zooms. Now this is causing another new problems as to render overlays and tiles at the same time. Ok leaving overlay aside, should I clear the tilecache at any time? – zIronManBox Jun 23 '14 at 04:23
  • And how can I load maptiles in the background using osmdroid? – zIronManBox Jun 23 '14 at 04:23
  • Could you help me with reusing the bitmaps here? using `inBitmap`? – zIronManBox Jun 23 '14 at 10:33