Search code examples
javaandroidclasscastexception

java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable


I'm trying to decompose a crash on my Android app from a device with the following specs:

Device
PAP3400DUO 1
Manufacturer — Android version Android 4.2 RAM (MB) — Screen size 480 × 800 Screen density (dpi) 240 OpenGL ES version 2.0 Native platform armeabi-v7a

Error:

java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
    at com.yitter.android.activity.EditProfileActivity.lambda$setSubmitProfileChangesClickListener$46(EditProfileActivity.java:120)
    at com.yitter.android.activity.EditProfileActivity.access$lambda$1(EditProfileActivity.java)
    at com.yitter.android.activity.EditProfileActivity$$Lambda$2.onClick(Unknown Source)
    at android.view.View.performClick(View.java:4212)
    at android.view.View$PerformClick.run(View.java:17476)
    at android.os.Handler.handleCallback(Handler.java:800)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5371)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)

Is it really pointing me to line 120? The code there shows this approximately:

ImageView imageView = (ImageView) findViewById(R.id.profile_picture);
            Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] image = stream.toByteArray();
            ParseFile file = new ParseFile("profilePicture.png", image);
            user1.put("profilePicture", file);

What could the problem possibly be? I researched the ClassCastException thrown but I can't seem to put the two together. What does drawable.ColorDrawable have to do with my bitmap here?

Edit:

<ImageView
                        android:id="@+id/profile_picture"
                        android:layout_width="87dp"
                        android:layout_height="87dp"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:clickable="true"
                        android:contentDescription="@string/content_desc_profile_picture"
                        android:scaleType="fitXY"
                        android:src="@android:color/transparent" />

Solution

  • Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    

    Apparently, imageView does not contain a BitmapDrawable, but rather a ColorDrawable, in the situation where this exception occurs. You need to examine all of the different places that you are populating this ImageView. Or, better yet, rather than try to yank a drawable back out of an ImageView, get the image from your model objects or whatever data source you are using to populate the ImageView in the first place.