TableLayout background from sdcard
I want to put a background image in may java file from an image stored on the sdcard. I u开发者_如何学运维se 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 ?
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" />
精彩评论