I want to Send some HTML
code from MainActivity
to a WebViewActivity
which have a WebView
layout, I want to get the HTML
code and display image on webview
. My HTML
code is :
<html><img alt=\"myImagenotshowing\" src=\"file://mnt/sdcard/HappyEnding.jpg\"></html>
And My WebViewActivity
is :
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);
}
}}
the above code does not display image from /mnt/sdcard/HappyEnding.jpg
. How to fix that..
CODE
this is my code sent from MainActivity
:
String value2 = "<html><img alt=\"myImagenotshowing\" src=\"file://mnt/sdcard/HappyEnding.jpg\"></html>";
intent = new Intent(MainActivity.this, WebViewActivity.class);
intent.putExtra(MY_EXTRA, value2);
startActivity(intent);
use this:
<html><img alt=\"myImagenotshowing\" src=\"file://mnt/sdcard/HappyEnding.jpg\"></html>
or
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", "");
add this permission:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Edited: Here is Full code of your answer : Its working find now.
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", "");
}
}
}
在webview中显示图像从SD卡读取src
<img style="width:100%" src="file:///mnt/sdcard/ax.png"/>
After search, i found that the solution is easier than Wasting 1 hours of search. Use wbView.loadDataWithBaseURL() instead of wbView.loadData()
webv.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.