来自 SD 卡的 TableLayout 背景

发布于 2024-09-16 16:22:29 字数 285 浏览 1 评论 0原文

我想将存储在 sdcard 上的图像中的背景图像放入可能的 java 文件中。 我使用下面的代码但没有成功:/

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

一个想法?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁虫ゝ 2024-09-23 16:22:29

您无法从 SD 卡获取文件作为资源。资源仅与 apk 捆绑。您必须从 SD 卡上的文件创建一个可绘制对象并使用它:

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

此外,您还必须请求访问 SD 卡的权限,在清单中添加:

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

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" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文