我正在尝试使用OSMdroid库显示带有折线的离线地图。一切顺利,没有什么版本的Android手机版。
然而,当我在三星s5或LG g4上运行代码时,代码停止运行且离线地图图块无法加载,只加载一半。我得到的例外是:OutofmemoryError加载位图,错误加载磁贴,低内存异常等。请帮我解决这个问题。 我很抱歉我的英语!谢谢! 我的代码:
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){
}
答案 0 :(得分:0)
在我的Android设备上,osmdroid将加载的磁贴缓存在/ storage / sdcard0 / osmdroid / tiles .....中。
如果/ storage / sdcard0 /上没有剩余空间,那么你会得到&#34;低内存异常&#34;
您必须从&#34; / storage / sdcard0 /&#34;中删除文件; ...