Android image can't convert to drawable

。_饼干妹妹 提交于 2020-04-30 20:33:08

问题


I am not able to add image. I downloaded the image and added it to res directory under drawable-mdpi. Then when I try to add the image on the view using imageView, it gives me this error

Couldn't resolve resource @drawable/ball01 
Failed to convert @drawable/ball01 into a drawable

Here is my xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_gravity="center"
        android:textSize="32sp"
        android:layout_above="@+id/button"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enlighten Me"
        android:id="@+id/button"

        android:layout_centerVertical="true"
        android:layout_alignParentRight="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_above="@+id/textView"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ball01" />


</RelativeLayout>

What am I doing wrong here.


回答1:


The directory name should be 'drawable-mdpi'. Using a hyphen instead of an underscore.




回答2:


image name must Digital or lowercase 。 ide only identification.




回答3:


I had this same problem, I had this image which I named Barcode and then saved it into my drawable folder, then I decided it would be better to have it named barcode with a small b, so I renamed it. Then I checked if it would work, and I got the same errors as you, after changing the name in my xml to 'Barcode' with a big B, it worked.

solution for me: delete barcode image from drawable and add it again with the name I'm going to use for it, don't change the name while it is in drawable folder.




回答4:


I had this problem on android studio. if you close and open project and it continues, all you need to do is RIGHT CLICK on the particular image you put in your "res" folder (that's giving the problem) and refactor-RENAME. Remove spaces in the name of the file. that should do it.



来源:https://stackoverflow.com/questions/21358776/android-image-cant-convert-to-drawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!