Android SD卡的TableLayout背景

Android SD卡的TableLayout背景,android,background,tablelayout,identifier,sd-card,Android,Background,Tablelayout,Identifier,Sd Card,我想在may java文件中放入一个背景图像,该图像来自SD卡上存储的图像。 我使用以下代码,但没有成功:/ TableLayout tl=new TableLayout(this); int tmp = this.getResources().getIdentifier("sdcard/pic.png", "drawable", getPackageName()); tl.setBackgroundResource(tmp); 想法?您无法从SD卡获

我想在may java文件中放入一个背景图像,该图像来自SD卡上存储的图像。 我使用以下代码,但没有成功:/

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

想法?

您无法从SD卡获取文件作为资源。资源仅与apk绑定。您必须从SD卡上的文件创建一个drawable,并使用该文件:

tl.setBackgroundDrawable(Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "pic.png").getAbsolutePath()));
您还必须申请访问SD卡的权限,在清单中添加:

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