I'm trying to set icon of a Marker in Google Maps v2. I'm downloading some images over network and change their background in code. After that I'm setting them as icons to markers. At first creation of the map it works fine but after rotation there is an exception.
Android version I run this on: 4.3
My code is as follows:
UrlImageViewHelper.loadUrlDrawable(TuvaletlerMapActivity.this,
iconUrl, new UrlImageViewCallback() {
@Override
public void onLoaded(ImageView iv, Bitmap bm,
String arg2, boolean arg3) {
Bitmap bitmap = VenuesHelper.iconizeBitmap(bm);
marker.setIcon(BitmapDescriptorFactory
.fromBitmap(bitmap));
}
});
and VenuesHelper.iconizeBitmap()
is as follows:
public static Bitmap iconizeBitmap(Bitmap bm) {
Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(),
bm.getConfig());
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.parseColor("#33B5E5"));
canvas.drawBitmap(bm, 0, 0, null);
return bitmap;
}
Stack trace is as follows:
08-07 10:16:50.684: E/AndroidRuntime(19001): FATAL EXCEPTION: main
08-07 10:16:50.684: E/AndroidRuntime(19001): java.lang.IllegalArgumentException: Released unknown bitmap reference
08-07 10:16:50.684: E/AndroidRuntime(19001): at maps.as.i.a(Unknown Source)
08-07 10:16:50.684: E/AndroidRuntime(19001): at maps.ah.o.b(Unknown Source)
08-07 10:16:50.684: E/AndroidRuntime(19001): at maps.ah.bn.a(Unknown Source)
08-07 10:16:50.684: E/AndroidRuntime(19001): at bix.onTransact(SourceFile:204)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.os.Binder.transact(Binder.java:347)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.google.android.gms.internal.dm$a$a.f(Unknown Source)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.google.android.gms.maps.model.Marker.setIcon(Unknown Source)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.behlul.tuvaletbul.TuvaletlerMapActivity$TuvaletliYukleCallbacks$1.onLoaded(TuvaletlerMapActivity.java:250)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$2.run(UrlImageViewHelper.java:615)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$3.onPostExecute(UrlImageViewHelper.java:653)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$3.onPostExecute(UrlImageViewHelper.java:1)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.os.AsyncTask.finish(AsyncTask.java:631)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.os.Looper.loop(Looper.java:137)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-07 10:16:50.684: E/AndroidRuntime(19001): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 10:16:50.684: E/AndroidRuntime(19001): at java.lang.reflect.Method.invoke(Method.java:525)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-07 10:16:50.684: E/AndroidRuntime(19001): at dalvik.system.NativeStart.main(Native Method)
A bitmap is simply a rectangle of pixels. Each pixel can be set to a given color but exactly what color depends on the type of the pixel. The first two parameters give the width and the height in pixels. The third parameter specifies the type of pixel you want to use.
You can use eraseColor on bitmap to set its color to Transparent. It will useable again without recreating it.
To create a bitmap from a resource, you use the BitmapFactory method decodeResource(): Bitmap bitmap = BitmapFactory. decodeResource(getResources(), R. drawable.
You can do a check when you want to return the BitMap look to see if the ArrayList of Paths is bigger than 0 and return the BitMap if so, or else return null.
I had a similar problem, when I tried to reload a Marker executing "myMarker.setIcon()" then after some refreshes the app ran into a "java.lang.IllegalArgumentException: Released unknown bitmap reference".
I discovered that calling "myMap.clear()" for cleaning all markers was the problem, In fact in the function docs you can read that "Removes all markers, polylines, polygons, overlays, etc from the map.".
Well, that "etc" seems to do more as I expected...
For solving that I used a custom function to iterate through all my markers saved in a HashMap and removing one by one, and that's all, no more exceptions like that in my code.
You can iterate through all markers to remove them as follows:
/**
* Alternative to myMap.clear() to avoid undesired exceptions
*/
private void clearAllMapMarkers() {
// Clearing the current map markers being shown
// Note that we do not use myMap.clear() because that incur in the exception
// "java.lang.IllegalArgumentException: Released unknown bitmap reference"
try {
for (Map.Entry<String, Marker> markerEntry : mMarkerList.entrySet()) {
markerEntry.getValue().remove();
}
} catch (IllegalArgumentException e) {
// Manage here the exception (never raised but who knows...)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With