Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jpeg files won't work as drawable resources - png files work fine

Tags:

android

I am trying to use a background image... when I place the file.png in the drawable folder, eclipse recognizes it and everything works.

However, file.jpg doesn't work.

Can someone please tell me what I'm doing wrong and how to resolve this?

I know it must work because in the docs it said the drawable resource can be a jpeg file also.

like image 481
ycomp Avatar asked Feb 14 '12 02:02

ycomp


People also ask

Which function is used is to load a drawable image resource?

Drawable myImage = ResourcesCompat. getDrawable(res, R. drawable. my_image, null);

Are PNG and JPEG the same?

Because of their different compression processes, JPEGs contain less data than PNGs — and therefore, are usually smaller in size. Unlike JPEGs, PNGs support transparent backgrounds, making them preferred for graphic design.

Is it better to export as PNG or JPEG?

You'll generally get better compression with JPEG than with PNG, but if quality is your priority you may want to stick with PNG since it's guaranteed to be lossless.

How do you insert a picture into drawable?

To import image resources into your project, do the following: Drag and drop your images directly onto the Resource Manager window in Android Studio. Alternatively, you can click the plus icon (+), choose Import Drawables, as shown in figure 3, and then select the files and folders that you want to import.


1 Answers

This is to clarify the issue for the other beginners out there that are running into this. This appears to be a rampant issue that gets most beginners and wastes a ton of time. Just google "eclipse load drawable" and look at all the people having trouble. They all end with "well, it works now but I don't know why...".

I've had the same issue on two different projects over the last couple days. There does appear to be an issue (undocumented feature?) with Eclipse. First, make sure the filename is in all lowercase. This wasn't immediately clear to me until i read [a-z1-9_.] about a hundred times. It does NOT in fact say [a-zA-Z1-9_.].

Second you need to restart Eclipse after adding drawable images to a project. Eclipse will not build the drawable resources during it's normal operation. The example of this is to simply add an image (png or jpg) and manually build the project. No matter how many times you build or clean the project, the images will not show up in /bin/res/drawable. Now save your project and click File->Restart. Auto-magically the images are in the bin folder, can be referenced using @drawable/, and do not cause build errors.

like image 150
Mike Avatar answered Oct 11 '22 14:10

Mike