我的资源文件夹中有一个jpg图像。现在我希望它能够显示在我的WebView中,以及来自数据库字段的HTML中的其他文本。我如何检索/显示该JPG图像?我如何在我的WebView中检索jpg图像android
回答
下面是用的icon.png图像被显示在web视图的完整代码。请查看放置在项目资产文件夹中的Demo.html。运行随附的项目并让我知道您是否有任何其他疑问。
WebViewDemo.java
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
/**
* Demonstrates how to embed a WebView in your activity. Also demonstrates how
* to have javascript in the WebView call into the activity, and how the activity
* can invoke javascript.
* <p>
* In this example, clicking on the android in the WebView will result in a call into
* the activities code in {@link DemoJavaScriptInterface#clickOnAndroid()}. This code
* will turn around and invoke javascript using the {@link WebView#loadUrl(String)}
* method.
* <p>
* Obviously all of this could have been accomplished without calling into the activity
* and then back into javascript, but this code is intended to show how to set up the
* code paths for this sort of communication.
*
*/
public class WebViewDemo extends Activity {
private static final String LOG_TAG = "WebViewDemo";
private WebView mWebView;
private Handler mHandler = new Handler();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
mWebView.setWebChromeClient(new MyWebChromeClient());
mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");
mWebView.loadUrl("file:///android_asset/demo.html");
}
final class DemoJavaScriptInterface {
DemoJavaScriptInterface() {
}
/**
* This is not called on the UI thread. Post a runnable to invoke
* loadUrl on the UI thread.
*/
public void clickOnAndroid() {
mHandler.post(new Runnable() {
public void run() {
mWebView.loadUrl("javascript:wave()");
}
});
}
}
/**
* Provides a hook for calling "alert" from javascript. Useful for
* debugging your javascript.
*/
final class MyWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.d(LOG_TAG, message);
result.confirm();
return true;
}
}
}
main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/intro"
android:padding="4dip"
android:textSize="16sp"
/>
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
</LinearLayout>
Demo.html
<html>
<script language="javascript">
/* This function is invoked by the activity */
function wave() {
alert("1");
document.getElementById("droid").src="android_waving.png";
alert("2");
}
</script>
<body>
<!-- Calls into the javascript interface for the activity -->
<a onClick="window.demo.clickOnAndroid()"><div style="width:80px;
margin:0px auto;
padding:10px;
text-align:center;
border:2px solid #202020;" >
<img id="droid" src="android_normal.png"/><br>
Click me!
</div></a>
</body>
</html>
的strings.xml
<resources>
<string name="intro">Demonstrates an embedded WebView and two-way communication between the HTML document and the hosting activity.</string>
<string name="app_name">WebViewDemo</string>
</resources>
你可以试试:
android.resource:// PACKAGE_NAME/ID_NUMBER
或
android.resource://软件包名称/类型/名称
请参阅[openAssetFileDescriptor()
] [1 ]更多。
[1]:http://developer.android.com/reference/android/content/ContentResolver.html#openAssetFileDescriptor(android.net.Uri,java.lang.String中)
你真正的意思是由android.resource:// package_name/id_number 和android.resource:// package_name/type/name? 我有com.xxxx.ebook作为我的包。请解释 – 2010-05-31 06:06:11
Phooey - 我的链接不起作用。试试这个:http://bit.ly/cGOYY0这就是关于这个话题AFAIK的所有文档。 – CommonsWare 2010-05-31 10:56:17
感谢Mark!在这个链接上有一个WebViewDemo项目,它在WebView中显示一个png图像。你能告诉我从哪里可以下载这个项目:http://code.google.com/p/apps-for-android/ – 2010-07-02 10:57:44
- 1. 如何在android中显示.jpg图像?
- 2. Android - 如何在WebView中叠加图像?
- 3. 无法使用php检索JPG图像
- 4. 我如何存储和检索数据库中的图像?
- 5. 如何从我的resx文件中检索图像?
- 6. 我无法上传我的表单中的JPG图像
- 7. 如何检索保存在android系统的photolibrary中的图像?
- 8. 如何在Latex中添加jpg图像
- 9. 我的webview的android
- 10. 在GWT中从我的sql中检索图像
- 11. 我怎么能存储图像和检索相同的sqlite-android
- 12. 我如何更改WebView的内容类型PostUrl()? Android WebView中的
- 13. Android Firebase ..我如何检索数据
- 14. 我如何检查图像的类型?
- 15. 检索库图像中的android
- 16. Android排球检索图像
- 17. 我不能上传PNG缩略图我上传JPG图像
- 18. 如何在android的webview中的imageview上设置图像
- 19. 在Android中,如何检查,如果我的PARAMS包含值图像
- 20. 如何检索javascript中的图像
- 21. 如何在android中的一个webview中打开我的链接?
- 22. Android:从webview中读取图像,将图像写入webview
- 23. 从我的Android应用程序中的SD卡中检索图像
- 24. 在jpg图像上绘制文本android
- 25. android,从资产中读取jpg图像
- 26. Twitter - 从webview中检索pin码。 Android
- 27. 如何检索库和存储图像在SQLite的android系统
- 28. 如何从Android中的图像文件检索元数据
- 29. 如何使用webview将我的服务器端的图像加载到我的android活动中?
- 30. 我们可以用JPG图像在iPhone应用程序中
这不是一个答案。 – noah 2010-08-14 00:19:36