0

I want to take a snapshot of the map when user select place from place picker activity. I'm storing this image to the SD card. But it gives very poor quality. I can't understand why? I've also set image quality to 100 in this line of code: bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);.

The snapshot looks like:

enter image description here

But I want little bit bigger image of good quality like one in the following image, which only contains text "Google":

enter image description here

I want the middle image of map which is displayed in the dialogue. I'm following this answer.

My code of handling the image and taking snapshot is as following:

 //opening place picker activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == PLACE_PICKER_REQUEST
            && resultCode == Activity.RESULT_OK) {

        final Place place = PlacePicker.getPlace(this, data);
        final CharSequence name = place.getName();
        final CharSequence address = place.getAddress();



        String attributions = (String) place.getAttributions();
        if (attributions == null) {
            attributions = "";
        }
     //   tv4.setText(place.getLatLng().toString()+"\n"+name+"\n"+address+"\n"+attributions);  To get latitide and longitudes.
        tv4.setText(address+"\n"+attributions);

        LatLngBounds selectedPlaceBounds = PlacePicker.getLatLngBounds(data);
        // move camera to selected bounds
        CameraUpdate camera = CameraUpdateFactory.newLatLngBounds(selectedPlaceBounds,0);
        mMap.moveCamera(camera);

        // take snapshot and implement the snapshot ready callback
        mMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
            Bitmap bitmap=null;
            public void onSnapshotReady(Bitmap snapshot) {
                // handle snapshot here
                bitmap = snapshot;
                try {
                    FileOutputStream out = new FileOutputStream("/mnt/sdcard/Download/000p.png");
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                    Toast.makeText(Main2Activity.this,"dsfds",Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(Main2Activity.this,e.toString(),Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }

        });
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

What should I change to achieve this? Is there any other way to get this snapshot?

Thanks for help!

Community
  • 1
  • 1

1 Answers1

0

Not sure of this, but it might be that the CameraUpdate is not done when you take the snapshot.

You could try this:

boolean placePicked = false;

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == PLACE_PICKER_REQUEST && resultCode == Activity.RESULT_OK) {
        placePicked = true;
        // Do everything else, except taking the snapshot.
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

@Override
public void onCameraIdle() {
    if (placePicked) {
        placePicked = false;

        // take snapshot...
    }
}

onCameraIdle is the method called when there was a CameraUpdate and it is finished.

Your activity should implement GoogleMap.OnCameraIdleListener, and add to your map mMap.setOnCameraIdleListener(this); inside onMapReady().

Also, I'm assuming your map is already loaded. If not, you should account for that using onMapLoaded().

jpvillegas
  • 93
  • 1
  • 9
  • Sure, what do you want to know? At the moment you call `mMap.move(cameraUpdate)` the map has to move and that takes time. So if you call `mMap.snapshot(...)` immediately after `mMap.move(...)` the map might not be ready. I have not tested this, it is just a theory, but you might want to try what I have suggested in the answer. – jpvillegas Sep 02 '16 at 13:08