0

I have this series of xml-drawables in particular drawable folders, for example in drawable-mpdi:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/m0_48px"
/> 

drawable-hdpi differs only with src pointing to m0_72px and so on.

And I want to let android decide what image resolution will use in marker. If I do this in this way:

int imageRes = c.getResources().getIdentifier("drawable/m0",
                    null, c.getPackageName());
Marker marker = m.addMarker(new MarkerOptions()
            .position(new LatLng(s.lat, s.lon))
            .icon(BitmapDescriptorFactory.fromResource(imageRes))
            .anchor(0.5f, 1f));

I get some resource ID in imagesRes, but BitmapDescriptorFactory won't accept this.

If I do it in this way:

int imageRes = c.getResources().getIdentifier("drawable/m0",
                        null, c.getPackageName());
Bitmap bitmap = BitmapFactory.decodeResource(c.getResources(), imageResource);

Marker marker = m.addMarker(new MarkerOptions()
    .position(new LatLng(s.lat, s.lon))
    .icon(BitmapDescriptorFactory.fromResource(imageRes))
    .anchor(0.5f, 1f));

I get null in bitmap var.

This question Set Image from drawable as marker in Google Map version 2 and especially this comment

Well, you can - you just need to paint it into a Canvas first (drawable.draw(canvas)), then dump the Canvas to a Bitmap. – Chris Broadfoot

lead me nowhere I'm not able to replicate mentioned procedure...

Please, any help welcome!

Community
  • 1
  • 1
Petr Marek
  • 1,381
  • 1
  • 15
  • 31

2 Answers2

2
icon(BitmapDescriptorFactory.fromResource(R.drawable.your_icon))

should work just fine. If the icon has the same name in each of the folders (mdpi, hdpi...) android will choose the right one to use. Why do you have different names for the marker icon?

andrei
  • 2,934
  • 2
  • 23
  • 36
  • 1) the thing is that I have images only in drawable folder and in hdpi, xhdpi,.. I have xml (as above) pointing to particular image in needed resolution 2) it indicates some amount on particular position – Petr Marek May 06 '14 at 15:04
-3

This should work fine, I know google maps is using the density of the image, but Android will decide which image to use, once you try to decode the source.

    Bitmap bitmap = BitmapFactory.decodeResource(c.getResources(), R.drawable.icon);
    Marker marker = m.addMarker(new MarkerOptions()
    .position(new LatLng(s.lat, s.lon))
    .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
    .anchor(0.5f, 1f));

The drawable must point to either a PNG drawable or a drawable that has a bitmap as its root.

Meanman
  • 1,474
  • 20
  • 17
Rodolfo Abarca
  • 565
  • 7
  • 15
  • 1
    This doesn't work and crashes with an exception: com.google.maps.api.android.lib6.common.apiexception.b: Failed to decode image. The provided image must be a Bitmap. – Meanman Oct 20 '17 at 11:11
  • @Meanman are you sure that is not your code, or something in your structure which is failing? Your exception is related to the decode image which should be a Bitmap object, Bitmap object can be obtain from different ways – Rodolfo Abarca Oct 21 '17 at 13:23
  • 1
    It will work only if the drawable is a PNG drawable or a drawable that has a bitmap as its root. – Meanman Oct 21 '17 at 20:09