I have trouble with this simple piece of code.
I download an image from the web and save it locally with :
File mFile = new File(context.getFilesDir(), "a.png");
if(!mFile.exists()) { mFile.createNewFile(); }
FileOutputStream fos = new FileOutputStream(mFile);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
I try to display this image on the ImageView and it's working.
Now i try to display the save image on a WebView.
String data = "<body>" +"<img src=\"a.png\"/></body>";
webview.loadDataWithBaseURL(getActivity().getFilesDir().toString(),data , "text/html", "utf-8",null);
It's not working, the webview shows nothing. I try the webview with a png i put myself in /assets and it's working.
I think my syntax to point to the file in the String data is wrong but i'm not sure.
Any help appreciated.
Thanks.
Alex.
Ok, after testing lots of different things, i end up with this working code.
WebView webview = (WebView) view.findViewById(R.id.imageView);
try {
FileInputStream in = getActivity().openFileInput("image_behindfragment.png");
BufferedInputStream buf = new BufferedInputStream(in);
byte[] bitMapA= new byte[buf.available()];
buf.read(bitMapA);
Bitmap bM = BitmapFactory.decodeByteArray(bitMapA, 0, bitMapA.length);
//imageview.setImageBitmap(bM);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
String imgToString = Base64.encodeToString(bitMapA,Base64.DEFAULT);
String imgTag = "<img src='data:image/png;base64," + imgToString
+ "' align='left' bgcolor='ff0000'/>";
webview.getSettings().setBuiltInZoomControls(true);
webview.setInitialScale(30);
WebSettings webSettings = webview.getSettings();
webSettings.setUseWideViewPort(true);
webview.loadData(imgTag, "text/html", "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file://"+ base + "/a.png";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
WebView.loadDataWithBaseURL("file:///mnt/sdcard/Your Folder/", html, "text/html","utf-8", "");