1. 程式人生 > >自留:Osm地圖osmdroid下載離線地圖,並將下載的地圖型別轉換為圖片(.png/.jpg)

自留:Osm地圖osmdroid下載離線地圖,並將下載的地圖型別轉換為圖片(.png/.jpg)

 

需要自定義Writer實現IFilesystemCache

public class ImgTileWriter implements IFilesystemCache {
    private String dir;

    public ImgTileWriter(String cacheDir) {
        if (!cacheDir.endsWith(File.separator)) {
            cacheDir += File.separator;
        }
        dir = cacheDir;
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    @Override
    public boolean saveFile(final ITileSource pTileSourceInfo, final long pMapTileIndex, final InputStream pStream, final Long pExpirationTime) {
        FileOutputStream bos = null;
        File file = null;
        try {
            int zoom = MapTileIndex.getZoom(pMapTileIndex);
            int x = MapTileIndex.getX(pMapTileIndex);
            int y = MapTileIndex.getY(pMapTileIndex);
            file = new File(dir + ""+x +""+y+".png");
            File parent = file.getParentFile();
            if (!parent.exists()) parent.mkdirs();
            if (file.exists()) {
                return true;
            }

            bos = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = pStream.read(buffer, 0, 8192)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
            bos.close();
            pStream.close();
        } catch (Throwable ex) {
            if (file != null) file.delete();
            Log.e("niupi", "Unable to store cached tile from " + pTileSourceInfo.name() + " " + MapTileIndex.toString(pMapTileIndex), ex);
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }


    @Override
    public boolean exists(ITileSource pTileSource, final long pMapTileIndex) {
        int x = MapTileIndex.getX(pMapTileIndex);
        int y = MapTileIndex.getY(pMapTileIndex);
       // File file = new File(dir + DataTool.getMapCacheName(x, y, zoom));
        File file = new File(dir + ""+x +"/"+y+".png");
        return file.exists();
    }

    @Override
    public void onDetach() {

    }

    @Override
    public boolean remove(final ITileSource tileSource, final long pMapTileIndex) {
        //not supported
        return false;
    }

    @Override
    public Long getExpirationTimestamp(final ITileSource pTileSource, final long pMapTileIndex) {
        return null;
    }


    /**
     * @since 5.6.5
     */
    @Override
    public Drawable loadTile(final ITileSource pTileSource, final long pMapTileIndex) throws Exception {
        InputStream inputStream = null;
        try {
            int x = MapTileIndex.getX(pMapTileIndex);
            int y = MapTileIndex.getY(pMapTileIndex);
           // File file = new File(dir + DataTool.getMapCacheName(x, y, zoom));
            File file = new File(dir + ""+x +""+y+".png");
            inputStream = new FileInputStream(file);
            return pTileSource.getDrawable(inputStream);
        } finally {
            if (inputStream != null) {
                StreamUtils.closeStream(inputStream);
            }
        }
    }
}

 

 

在MainActivity新增點選按鈕呼叫下面的方法

CacheManager mgr = null;
ImgTileWriter writer=null;
private void osmTileDownload() {
    try {
            String outputName = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +
                    "osmdroid" + File.separator +"osm";
            writer = new  ImgTileWriter(outputName);
            mgr = new CacheManager(mMapView, writer);
            int zoommin = 1;
            int zoommax = 12;
            //下載區域
             BoundingBox bb = new BoundingBox(23.97956070462608, 113.9886474609375, 21.10841669888532, 112.0111083984375);//北東南西
            mgr.downloadAreaAsync(getApplicationContext(), bb, zoommin, zoommax, new CacheManager.CacheManagerCallback() {
                @Override
                public void onTaskComplete() {
                    Toast.makeText(getApplicationContext(), "Download complete!", Toast.LENGTH_LONG).show();
                    if (writer != null)
                        writer.onDetach();
                }

                @Override
                public void onTaskFailed(int errors) {
                    Toast.makeText(getApplicationContext(), "Download complete with " + errors + " errors", Toast.LENGTH_LONG).show();
                    if (writer != null)
                        writer.onDetach();
                }

                @Override
                public void updateProgress(int progress, int currentZoomLevel, int zoomMin, int zoomMax) {
                    //NOOP since we are using the build in UI
                }

                @Override
                public void downloadStarted() {
                    //NOOP since we are using the build in UI
                }

                @Override
                public void setPossibleTilesInArea(int total) {
                    //NOOP since we are using the build in UI
                }
            });



} catch (Exception ex) {
    //TODO something better?
    ex.printStackTrace();
}
}

參考文章:https://blog.csdn.net/z19980115/article/details/79472005#comments