我的資源文件夾中有一個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