Android setImageUri 不适用于资产 Uri
我使用以下代码从资产文件夹中设置图像。
Uri numBgUri = Uri.parse("file:///android_asset/background_numbers.png");
numBgImage.setImageURI(numBgUri);
background_numbers.png 文件肯定存在于 asset 根目录中。我在日志中收到 FileNotFoundException: -
09-23 17:05:23.803: WARN/ImageView(23713): Unable to open content: file:///android_asset/background_numbers.png
有什么想法我可能做错了吗?
谢谢!
I am using the following code to set an image from the assets folder.
Uri numBgUri = Uri.parse("file:///android_asset/background_numbers.png");
numBgImage.setImageURI(numBgUri);
The background_numbers.png file definitely exists in the assets root directory. I am getting a FileNotFoundException in the log: -
09-23 17:05:23.803: WARN/ImageView(23713): Unable to open content: file:///android_asset/background_numbers.png
Any ideas what I could be doing wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我设法使用我自己的自定义 ContentProvider 找到了一个可行的解决方法(这比您想象的更容易!)。感谢Google 网上论坛上的skink提供的提示
此代码应该可以工作,您只需要设置一个提供程序 URL 并将其注册为清单文件中的提供程序
我希望这可以帮助任何遇到同样问题的 Google 用户!
I managed to get a viable workaround for this using my own custom ContentProvider (It's easier than you think!). Thanks to skink on Google Groups for the tip
This code should work, you just need to set a provider URL and register it as a provider in the manifest file
I hope this helps any googlers out there with the same problem!
您需要将所有图像放入资源目录中。例如:
您可以通过以下方式访问它:
You need to place all images in you resources directory. For example :
And you can access it via:
我建议您仔细检查:
logcat 中出现的 ImageView 活动与您的代码属于同一个 apk。资产文件只能从您的应用程序内读取。
资产文件夹位于项目根目录中。
编辑
似乎不可能提供 ImageView 直接来自资产资源。请参阅此问题进行说明。
作为解决方法,您应该尝试从资源文件创建一个可绘制对象,然后使用 ImageView.setImageDrawable:
此代码的第一行是 这个答案来自@伊戈尔·霍缅科。
I advice you to double check that:
ImageView activity that appears in your logcat belongs to the same apk that your code. asset files are only readable from within your application.
The assets folder is in the project directory root.
EDIT
It seems that it is not possible to feed an ImageView directly from an asset ressource. See this question for illustration.
As a workaround you should try to create a drawable from your asset file, then associate it with your Image view using ImageView.setImageDrawable:
The first line of this code is an adaptation of this answer from @IgorKhomenko .