private void createMapViewImage(){ final Runnable runnable = new Runnable() { int counter = 0; @Override public void run() { counter++; try{ final Handler handler = new Handler(); //Use 0, 0 for x/y so that you capture the entire MapView final Bitmap bitmap = mMapView.getDrawingMapCache(0, 0, mMapView.getWidth(), mMapView.getHeight()); Log.d("TEST", "Map Width = " + mMapView.getWidth() + " Map Height = " + mMapView.getHeight()); Log.d("TEST","Testing if able to create mapView bitmap. Attempt #" + counter); if(bitmap != null){ Log.d("TEST","mapView bitmap has been successfully created."); final File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); final File output = new File(dir, "mapview.png"); final String imagePath = output.getAbsolutePath(); FileOutputStream fileOut; try{ fileOut = new FileOutputStream(output); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOut); fileOut.flush(); fileOut.close(); bitmap.recycle(); }catch(Exception e){ e.printStackTrace(); } handler.post(mUpdateResults); } else if(counter < 5){ handler.postDelayed(this, 2000); } else{ Log.d("TEST","Unable to create mapView bitmap after 5 attempts."); } } catch(Exception exc){ Log.d("TEST","Unable to create mapView exception: " + exc.toString()); } } }; runnable.run(); } final Runnable mUpdateResults = new Runnable() { @Override public void run() { doSomething(); } }; private void doSomething(){ final File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/mapview.png"); //You are now back on the UI thread and have access to the image file . }
private void createMapViewImage(){ final Runnable runnable = new Runnable() { int counter = 0; @Override public void run() { counter++; try{ final Handler handler = new Handler(); //Use 0, 0 for x/y so that you capture the entire MapView final Bitmap bitmap = mMapView.getDrawingMapCache(0, 0, mMapView.getWidth(), mMapView.getHeight()); Log.d("TEST", "Map Width = " + mMapView.getWidth() + " Map Height = " + mMapView.getHeight()); Log.d("TEST","Testing if able to create mapView bitmap. Attempt #" + counter); if(bitmap != null){ final File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); final File output = new File(dir, "mapview.png"); final String imagePath = output.getAbsolutePath(); FileOutputStream fileOut; try{ fileOut = new FileOutputStream(output); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOut); int w = bitmap.getWidth(); if(w > 0){ fileOut.flush(); fileOut.close(); bitmap.recycle(); handler.post(mUpdateResults); Log.d("TEST","mapView bitmap has been successfully created."); } else if(counter < 5){ Log.d("TEST","PNG didn't create properly so trying again."); handler.postDelayed(this, 1000); } else{ Log.d("TEST","Unable to create PNG."); } }catch(Exception e){ e.printStackTrace(); } } else if(counter < 5){ handler.postDelayed(this, 2000); } else{ Log.d("TEST","Unable to create mapView bitmap after 5 attempts."); } } catch(Exception exc){ Log.d("TEST","Unable to create mapView exception: " + exc.toString()); } } }; runnable.run(); } final Runnable mUpdateResults = new Runnable() { @Override public void run() { doSomething(); } }; private void doSomething(){ final File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/mapview.png"); //TO-DO }
Thank you, this works for me. However, it takes some time to produce the image. I am trying to perform the operation at a fast rate and it is introducing lag into my application. Is there any way to perform this method of turning the map view into an image so that it does not decrease the performance of my application? As in, it allows other operations to continue while multiple screenshots or generated, etc.
Blake, in the latest Android Runtime there is a method you might try: GeoView (ArcGIS Runtime SDK for Android 100.5.0). FYI, I haven't used the method since I haven't been working on Runtime in quite some time so I won't be able to answer any questions about it.