31

How to load html files in WebView from raw folder. I don't know how to add the path of these html files in WebView.loadData() function. I know how to load file from asset folder in WebView but here getting problem with raw folder.

Thanks in advance

yennsarah
  • 5,383
  • 2
  • 25
  • 47
unflagged.destination
  • 1,536
  • 3
  • 17
  • 37

1 Answers1

59

Use this code for Load html files from raw folder in web view:

For more information see the javadoc for WebView:

     public class ViewWeb extends Activity {  
            @Override  
            public void onCreate(Bundle savedInstanceState) {  
                super.onCreate(savedInstanceState);
                setContentView(R.layout.webview);  

                WebView lWebView = (WebView)findViewById(R.id.webView);
                lWebView.loadUrl("file:///android_res/raw/your_file_name.html");
            }  
        }
Dixit Patel
  • 6,179
  • 1
  • 31
  • 42
  • 9
    The problem is solved with WebView.loadUrl("file:///android_res/raw/file_name.html"); – unflagged.destination Jan 05 '13 at 11:47
  • 1
    It actually takes the correct raw folder, for example for different languages. That surprised me since the path looks very absolute. Thanks. – plexus Sep 01 '16 at 15:58
  • 2
    Be aware that a workaround is needed when the _aplication id_ is different in a _buildVariant_, like for example [with a suffix](http://stackoverflow.com/questions/26777566/file-under-res-raw-not-accessible-in-debug-buildvariant). – Sebastian Apr 24 '17 at 16:38
  • This solution doesn't work in release mode for me, The Gradle resource shrinker removes resources that are not referenced by the app code. https://developer.android.com/studio/build/shrink-code#unused-alt-resources – JIE WANG Feb 12 '21 at 13:17
  • 1
    @JIEWANG you need to add a file called keep.xml to the raw folder with the following content to keep your resources: – J. Hegg Mar 26 '21 at 22:09