views:

20

answers:

1

hey :)

I want to put a background image in may java file from an image stored on the sdcard. I use the code below but with no success :/

       TableLayout tl=new TableLayout(this);
       int tmp = this.getResources().getIdentifier("sdcard/pic.png", "drawable", getPackageName());
       tl.setBackgroundResource(tmp);

an idea ?

+1  A: 

You can't obtain file from SD card as resources. Resources are bundled with apk only. You have to create a drawable from a file on sdcard and use that:

tl.setBackgroundDrawable(Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "pic.png").getAbsolutePath()));

Also you have to ask for permission to access SD card, add at manifest:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Konstantin Burov
works very well, thanks :)
GeeXor