Bug 25890 - Image not loading into WebView with source of /data/data/<app>/files/
Summary: Image not loading into WebView with source of /data/data/<app>/files/
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 4.20.0
Hardware: PC Mac OS
: Highest blocker
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2015-01-09 15:22 UTC by Jon Goldberger [MSFT]
Modified: 2016-03-01 00:28 UTC (History)
8 users (show)

Is this bug a regression?: ---
Last known good build:

Test Project (886.51 KB, application/zip)
2015-01-09 15:22 UTC, Jon Goldberger [MSFT]

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jon Goldberger [MSFT] 2015-01-09 15:22:12 UTC
Created attachment 9308 [details]
Test Project

## Description

In the MainActivity ONCreate method, an image file is copied for the Assets folder to the System.Environment.SpecialFolder.Personal. After the file is copied, an attempt is made to load it into a WebView with:

>var myhtml = "<html> <Title>this is my html</Title> <body> This is my HTML <img src=\"chrysanthemum.jpg\" alt=\"chrysanthemum.jpg\" height=\"55\" width=\"55\" /> </body> </html>";

>// NOTE: I am trying to load image from /data/data/app/files/ and it fails
>string mypath = System.IO.Path.Combine("file://" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/");
>// NOTE: if I use the below to load image from assets then it works.
>//string mypath = "file:///android_asset/";
>webView.LoadDataWithBaseURL(mypath, myhtml, "text/html", "UTF-8", null);

The WebView loads but the image is shown with a "broken link" image.

## Steps to reproduce

1. Open the attached test project
2. Launch to an Android device or emulator (So far tested on Samsung Galaxy Tab 2 running v. 4.2.2 and Xamarin Android Player's Nexus 4 (KitKat) emulator.

Expected result: Webview will be displayed with a pretty picture of a Chrysanthemum.

Actual result: no image is displayed.

## Notes

I tried to place the Chrysanthemum.jpg file directly into the /data/data/<app>/files folder using my device's file browser to see if the error was in reading or writing the file, but still the image did not display.

No exception is thrown after the write operation, so it would seem the file is getting written, though I was unable to find it in the file browser, but that could be due to the protected nature of the app's files folder, though I do see the ._override_ folder that Xamarin adds to the files folder.I also looked through the logcat output and no errors or exceptions were noted in the log. 

## Version info:

Microsoft Visual Studio Ultimate 2013
Version 12.0.31101.00 Update 4
Microsoft .NET Framework
Version 4.5.51641

Installed Version: Ultimate

Architecture and Modeling Tools   06181-004-0451031-02209
Microsoft Architecture and Modeling Tools
UML® and Unified Modeling Language™ are trademarks or registered trademarks of the Object Management Group, Inc. in the United States and other countries.

LightSwitch for Visual Studio 2013   06181-004-0451031-02209
Microsoft LightSwitch for Visual Studio 2013

Team Explorer for Visual Studio 2013   06181-004-0451031-02209
Microsoft Team Explorer for Visual Studio 2013

Visual Basic 2013   06181-004-0451031-02209
Microsoft Visual Basic 2013

Visual C# 2013   06181-004-0451031-02209
Microsoft Visual C# 2013

Visual C++ 2013   06181-004-0451031-02209
Microsoft Visual C++ 2013

Visual F# 2013   06181-004-0451031-02209
Microsoft Visual F# 2013

Visual Studio 2013 Code Analysis Spell Checker   06181-004-0451031-02209
Microsoft® Visual Studio® 2013 Code Analysis Spell Checker

Portions of International CorrectSpell™ spelling correction system © 1993 by Lernout & Hauspie Speech Products N.V. All rights reserved.

The American Heritage® Dictionary of the English Language, Third Edition Copyright © 1992 Houghton Mifflin Company. Electronic version licensed from Lernout & Hauspie Speech Products N.V. All rights reserved.

Windows Phone SDK 8.0 - ENU   06181-004-0451031-02209
Windows Phone SDK 8.0 - ENU

Application Insights Tools for Visual Studio Package   1.0
Application Insights Tools for Visual Studio

ASP.NET and Web Tools   12.4.51016.0
Microsoft Web Developer Tools contains the following components:
Support for creating and opening ASP.NET web projects
Browser Link: A communication channel between Visual Studio and browsers
Editor extensions for HTML, CSS, and JavaScript
Page Inspector: Inspection tool for ASP.NET web projects
Scaffolding: A framework for building and running code generators
Server Explorer extensions for Microsoft Azure Websites
Web publishing: Extensions for publishing ASP.NET web projects to hosting providers, on-premises servers, or Microsoft Azure

ASP.NET Web Frameworks and Tools 2012.2   4.1.21001.0
For additional information, visit http://go.microsoft.com/fwlink/?LinkID=309563

ASP.NET Web Frameworks and Tools 2013   5.2.21010.0
For additional information, visit http://www.asp.net/

Common Azure Tools   1.3
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Microsoft Advertising SDK for Windows Phone   
Microsoft Advertising SDK for Windows Phone

Microsoft Azure HDInsight HQL Service   1.0.0000.0
Language service for Hive query

Microsoft Azure HDInsight Tools for Visual Studio   1.0.0000.0
An integrated development environment for HDInsight application development.

Microsoft Azure Mobile Services Tools   1.3
Microsoft Azure Mobile Services Tools

Microsoft Azure Tools   2.5
Microsoft Azure Tools for Microsoft Visual Studio 2013 - v2.5.21104.1601

Microsoft Azure Tools   2.4
Microsoft Azure Tools for Microsoft Visual Studio 2013 - v2.4.20730.1601

NuGet Package Manager   2.8.50926.663
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

Office Developer Tools for Visual Studio 2013 ENU   12.0.31105
Microsoft Office Developer Tools for Visual Studio 2013 ENU

PowerShell Tools   1.3
Provides file classification services using PowerShell

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SAPReferenceManager   1.0
Manages References to Shared Projects

SQL Server Data Tools   12.0.41025.0
Microsoft SQL Server Data Tools

Windows Azure Tools   2.3
Windows Azure Tools for Microsoft Visual Studio 2013 - v2.3.20320.1602

Windows Phone 8.1 SDK Integration   1.0
This package integrates the tools for the Windows Phone 8.1 SDK into the menus and controls of Visual Studio.

Workflow Manager Tools 1.0   1.0
This package contains the necessary Visual Studio integration components for Workflow Manager.

Xamarin (10cfd178d55287f09c85f5a1e604dfe20889a40f)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android (ba9bbbdd44cfdc4bf485e8885bd2ad24fba525f7)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.iOS (840a925103a0bf4a856507f13d5eaee3c1579c2f)
Visual Studio extension to enable development for Xamarin.iOS.
Comment 4 Jon Goldberger [MSFT] 2016-03-01 00:05:19 UTC
So this seemed to be a two-fold issue, neither of which is a Xamarin Android bug:

First, you need to use external storage as the app's private storage is not readable by the WebView (separate process).

Also the code to copy the file was incorrect and a StreamReader and StreamWriter are meant to deal with text streams, but (thanks Alan M) this works:

>FullFilePath = System.IO.Path.Combine((string)Android.OS.Environment.ExternalStorageDirectory, "Chrysanthemum.jpg");
> Application.Context.Assets.Open("Chrysanthemum.jpg").CopyTo (File.OpenWrite (FullFilePath));

You also need to modify the path for the baseUrl for the WebView:

string mypath = "file://" + (string)Android.OS.Environment.ExternalStorageDirectory  + "/";

(or some more elegant variation of the above that results in the same string.)

Full code for MainActivity with the above changes: 
Comment 5 Jon Goldberger [MSFT] 2016-03-01 00:28:26 UTC

Correction, the issue was the copy code only. 

If I only replace:

>if (!File.Exists(FullFilePath))
>    Reader = new StreamReader(Application.Context.Assets.Open("Chrysanthemum.jpg"));
>    Content = Reader.ReadToEnd();
>    Writer = new StreamWriter(FullFilePath);
>    Writer.Write(Content);
>    Reader.Close();
>    Writer.Close();


>if (!File.Exists(FullFilePath))
>    Application.Context.Assets.Open("Chrysanthemum.jpg").CopyTo (File.OpenWrite (FullFilePath));

then the test project works as expected. Gist updated: