Monday, 8 August 2011

Android - Load a image in a file (not drawable) as image in the layout

Sometimes, it is easier load a image from other directory (sdcard, for example) than load a image from drawable because for this last case, the image should be stored in the directory res/drawable first and packaged along with the apk files.

To load a image from a directory, it should be converted to the drawable first. Here comes a piece of code which can help:

File file = new File ("/sdcard/1.jpg");
ImageView imageView = (ImageView) findViewById(;

Be warning that there is another method for ImageView called setImageURI(URI uri). This method is used to load external files, it doesn't work with the type file. For example, this code won't work:

File file = new File ("/sdcard/1.jpg");
ImageView imageView = (ImageView) findViewById(;

Thanks Martin Wibbels for his post:

More information:
Android official Reference about ImageView:
Android official Reference about Drawable:

No comments:

Post a Comment