2010-08-30 51 views
2

我想從SD卡上存儲的圖像中放置一個可能在java文件中的背景圖像。 我用下面的代碼,但沒有成功:/來自SD卡的TableLayout背景

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

的想法?

回答

3

無法從SD卡獲取文件作爲資源。資源僅與apk捆綁在一起。你必須建立從SD卡文件的繪製和使用:

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

另外你要問權限訪問SD卡,加在清單:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

工作得非常好,謝謝: ) – Pachanka 2010-08-30 13:49:19