我想将HTML
的{{1}}代码发送到MainActivity
的{{1}}布局,我希望得到WebViewActivity
代码并显示WebView
上的图片。
我的HTML
代码是:
webview
我的HTML
是:
<html><img alt=\"myImagenotshowing\" src=\"file://mnt/sdcard/HappyEnding.jpg\"></html>
上述代码不显示WebViewActivity
的图片。
如何解决..
public class WebViewActivity extends Activity {
@SuppressWarnings("unused")
private static String TAG = "WebViewActivity";
private WebView webView;
public static final String MY_EXTRA = "fulltext";
Bundle extras;
String value;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
value = getIntent().getStringExtra(MY_EXTRA);// Here it gets HTML code that I want to display inwebview
Log.w(MY_EXTRA, "Data recieved is >> " + value);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.webview);
// Makes Progress bar Visible
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadData(value, "text/html", null);
return true;
}
});
new GetData().execute(null, null, null);
}
private class GetData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
webView.getSettings().setDomStorageEnabled(true);
}
@SuppressLint("SetJavaScriptEnabled")
@Override
protected Void doInBackground(Void... params) {
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(
false);
webView.getSettings().setSupportMultipleWindows(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setRenderPriority(
WebSettings.RenderPriority.HIGH);
webView.getSettings().setSavePassword(false);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setAppCacheMaxSize(200);
webView.getSettings().setDatabaseEnabled(true);
webView.clearCache(true);
// webView.clearHistory();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
webView.loadData(value, "text/html", null);
}
}}
这是我从/mnt/sdcard/HappyEnding.jpg
发送的代码:
CODE
答案 0 :(得分:0)
使用它:
<html><img alt=\"myImagenotshowing\" src=\"file://mnt/sdcard/HappyEnding.jpg\"></html>
或
String name = "/K/test.jpg";
webView = (WebView) findViewById(R.id.webView1);
String imagePath = "file://"+ Environment.getExternalStorageDirectory() + "/" + name;
String html = "<html><head></head><body><center><img src=\"" + imagePath + "\"></center></body></html>";
webv.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
添加此权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
编辑: 以下是您的答案的完整代码:现在正在寻找。
package com.example.testbrowser;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private static String TAG = "WebViewActivity";
private WebView webView;
private String value;
public static final String MY_EXTRA = "fulltext";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_Sherlock_Light);
// value = getIntent().getStringExtra(MY_EXTRA);
value = "<html><img alt='myImagenotshowing' src='file://mnt/sdcard/abc.jpg'></html>";
Log.e(MY_EXTRA, "Data recieved is >> " + value);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
// Makes Progress bar Visible
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
webView = (WebView) findViewById(R.id.webView1);
// webView.loadDataWithBaseURL("", value, "text/html", "utf-8", "");
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadDataWithBaseURL("", value, "text/html", "utf-8", "");
return true;
}
});
new GetData().execute(null, null, null);
}
private class GetData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
webView.getSettings().setDomStorageEnabled(true);
}
@SuppressLint("SetJavaScriptEnabled")
@Override
protected Void doInBackground(Void... params) {
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(
false);
webView.getSettings().setSupportMultipleWindows(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setRenderPriority(
WebSettings.RenderPriority.HIGH);
webView.getSettings().setSavePassword(false);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setAppCacheMaxSize(200);
webView.getSettings().setDatabaseEnabled(true);
webView.clearCache(true);
// webView.clearHistory();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
webView.loadDataWithBaseURL("", value, "text/html", "utf-8", "");
}
}
}
答案 1 :(得分:0)
在webview中显示图像从sd卡读取src
<img style="width:100%" src="file:///mnt/sdcard/ax.png"/>
答案 2 :(得分:0)
搜索后,我发现该解决方案比浪费1小时的搜索要容易。 使用wbView.loadDataWithBaseURL()代替wbView.loadData()
webv.loadDataWithBaseURL("", html, "text/html", "utf-8", "");