[英]Image from sd-card is not loaded in webview
我想將一些HTML
代碼從MainActivity
發送到具有WebView
布局的WebViewActivity
,我想獲取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
這是我從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);
用這個:
<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", "");
}
}
}
在webview中顯示圖像從SD卡讀取src
<img style="width:100%" src="file:///mnt/sdcard/ax.png"/>
搜索后,我發現該解決方案比浪費1小時的搜索要容易。 使用wbView.loadDataWithBaseURL()代替wbView.loadData()
webv.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.