Android: display images in Webview

After processing the file, I get an HTML string in which the image is set as

<img src="abc.001.png" width="135" height="29" alt="" style="margin-left:0pt; margin-top:0pt; position:absolute; z-index:-65536" />

The path to the image should not be changed, because I have to select a file element from the list. The image is in the same directory as the file. I am loading an HTML string using loadData / loadDataWithBaseURL but the image is not showing. I see only its frame.

How can i fix this? And can I apply this solution if I have many images indexed as .001.jpg, .002.png, etc. (Everything in the catalog)?

Update: thanks, it works with the loadUrl () operator no matter what I call the image. In fact, I have to read and process the content before uploading it to WebView. Therefore, I use the loadDataWithBaseUrl () statement and get the problems above. Here is my code in a test project for reading and displaying the contents of Test.html.

    String res = "";        
    File file = new File(Environment.getExternalStorageDirectory()+"/Test.html");
    try {
        FileInputStream in = new FileInputStream(file);

        if (in != null) {               
            BufferedReader buffreader = new BufferedReader(
                    new InputStreamReader(in));
            String line;
            while ((line = buffreader.readLine()) != null) {
                res += line;
            }
            in.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }       
    wv.loadDataWithBaseURL(null, res, "text/html", "utf-8", null);
  //wv.loadUrl("file://"+Environment.getExternalStorageDirectory()+"/Test.html");

The statement in // works, but that’s not what I can do in my real project. I have a solution: after processing the content, I have to save it in a temporary HTML file, and then upload it, this file will be deleted later. However, I am still waiting for a better solution :)

+5
source share
4 answers

Try changing the image file name. I thought this was due to a double dot in the name.

<img id="compassRose" src="CompassRose.jpg"></img>

it works for me ....

The code:

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebView;

public class StackOverFlowActivity extends Activity {

    private Handler mHandler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        WebView view=(WebView)findViewById(R.id.webView1);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl("file:///android_asset/index.html");
        view.addJavascriptInterface(new MyJavaScriptInterface(), "Android");
    }

    final class MyJavaScriptInterface
    {
        public void ProcessJavaScript(final String scriptname, final String args)
            {             
                mHandler.post(new Runnable()
                    {
                        public void run()
                            {
                                //Do your activities
                            }
                    });
            }
    }
}

index.html

<html>
<head>
  <title title="Index"></title>                                   
</head>

<body>
  <h2>Android App demo</h2>
  <br /> <img src="CompassRose.jpg" />
</body>
</html>

enter image description here

:

enter image description here

+9

:

wv.loadDataWithBaseURL(null, res, "text/html", "utf-8", null);

(baseURL) NULL. - WebView , URL-. , , ( ):

wv.loadDataWithBaseURL ("file://" + Environment.getExternalStorageDirectory(), res, "text/html", "utf-8", null);

, :

<uses-permission android:name="android.permission.INTERNET" />

, . , .

+1

:

webview.loadUrl("file:///android_asset/mypicture.jpg");
0
       WebView webView=(WebView)findViewById(R.id.my_wb);
        String url = "YOUR URL";
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.loadUrl(url);
0

All Articles