Android的:如何从RES /可绘制的图像,并把它变成WebView.loadUrl() [英] Android: how to retrieve an image from res/drawable and put it into WebView.loadUrl()
问题描述
我已经试过以下,但没有成功。
我的项目是使用API级别4。
I already tried the following but without success. My project is using API level 4.
<一个href=\"http://developer.android.com/reference/android/content/ContentResolver.html#openAssetFileDescriptor%28android.net.Uri,%20java.lang.String%29\"相对=nofollow>从Android文档
下面是我的code:
webView.loadUrl("android.resource://com.package.name.from.manifest/" + R.drawable.pic);
和我有在web视图控件以下错误:
And I've got the following error in the WebView widget:
找不到网页
在网页
android.resource://com.package.name.from.manifest/2130837504可能
暂时,或者它已永久性地移动到一个新的Web
地址。
The Web page at android.resource://com.package.name.from.manifest/2130837504 might be temporarily down or it may have moved permanently to a new web address.
我缺少的东西吗?我应该创建的HTML页面,其中将包括一个链接的资源为&lt; IMG ...>标签?
Am I missing something? Should I create html page which will include a link to the resource as < img ...> tag?
推荐答案
在最后,我也做了以下内容:
In the end I have done the following:
我每个资源的图像的文件名扩展的阵列。
然后我可以调用的资源ID下面的方法,它的文件扩展名。
I had an array of file name extensions of each of the image in resources. And then I could call the following method with the resource ID and it's file name extension.
protected String getResourceURL(int resourceId, String extension) {
String resName = getResources().getResourceName(resourceId);
int index = resName.indexOf("/");
resName = resName.substring(0, index);
index = resName.indexOf(":");
String resType = resName.substring(index + 1);
StringBuilder link = new StringBuilder(128);
link.append("file:///android_res/");
link.append(resType).append("/");
link.append(getResources().getResourceEntryName(resourceId));
link.append(".").append(extension);
return link.toString();
}
这篇关于Android的:如何从RES /可绘制的图像,并把它变成WebView.loadUrl()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!