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)
Found the solution. I was checking if the bitmap resides in the cache before reloading it, but I had forgotten to check for cache hit. Now I modified the code and it no longer crashes. I couldn't figure out the exact reason of the crash tough, probably some silly garbage-collection issues.
Here is the modified code which no longer crashes:
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: