6

I am making an application in which i want to open a local html page in the browser. I am able to open google.com. But when i'm opening local html file. But i am getting following error

The Request file was not found.

Following is my code:

try
    {
        Intent i = new Intent(Intent.ACTION_VIEW);
        File f=new File("file:///android_asset/www/trialhtml.html");
        Uri uri = Uri.fromFile(f);
        i.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
        i.setData(uri);
        startActivity(i);
    }
    catch(Exception e)
    {
      System.out.print(e.toString());   

    }
dziwna
  • 1,212
  • 2
  • 14
  • 25

5 Answers5

4

file:///android_asset/www/trialhtml.html means nothing to an external application like the Web Browser. Any files in your assets are not accessible by other applications. You have 2 options.

  1. Copy the html file to shared storage so that the the file can be accessed by the webbrowser.
  2. Implement a WebView within a new Activity or fragment in your application then webview.loadUrl("file:///android_asset/www/trialhtml.html");

You do not need to read the asset like other answers are instructing you. WebView will handle this all behind the scenes, including loading other assets like images

As a side note, if the web browser was able to read your files, you would not want to use

        i.setClassName("com.android.browser", "com.android.browser.BrowserActivity");

This is because you are explicitly asking for a certain browser, which may or may not be installed on the user's device. I'm reasonably sure this isn't the case on some modern Android devices that come with only Chrome installed. The correct usage would be something like

Uri uri = Uri.parse("http://www.example.com"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

By not explicitly setting the class and package name, this ensures that no matter which web browser is installed, the users default will be picked.

Mike dg
  • 4,648
  • 2
  • 27
  • 26
  • i'm quite new to the android development. I don't know how to copy the html file to shared storage, please tell me that. –  Feb 08 '13 at 13:27
  • This seems not working for me, says www is an invalid resource directory name. I have also tried to use 'raw' instead but just says "web page is not available". Forced to stick with my streams. – Audrius Meškauskas Feb 08 '13 at 13:30
  • @sahilvig http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard has a great answer to that question. Be sure to not pollute the user's root of shared storage. – Mike dg Feb 08 '13 at 13:33
  • @AudriusMeškauskas do you actually have a www directory in your assets folder? The code you posted below wouldn't work if that was the case, and if that wasn't the case my code won't work. – Mike dg Feb 08 '13 at 13:34
  • Save html page to the other location. (www is the folder name, i have created) –  Feb 08 '13 at 13:35
  • As soon as I create the www folder in my assets (I mean under res/ in the Android project tree) it simply does not build any longer with "invalid resource directory name" error message. This approach probably cannot be used with assets but maybe can be used to open file from some shared location. Anyway you need to extract it from resources first. – Audrius Meškauskas Feb 08 '13 at 13:36
  • I have tried the same approach with the webview and it was working perfectly fine. Try creating folder www under assest and in this folder add your html file –  Feb 08 '13 at 13:41
  • @AudriusMeškauskas perhaps you are using an out of date SDK? – Mike dg Feb 08 '13 at 13:42
  • 1
    @AudriusMeškauskas perhaps you shouldn't place your asset folder under res. The asset folder should be on the root directory of your project. – Tim Roes Feb 08 '13 at 13:44
  • Figured out, must be in the folder called "assets" under project root. – Audrius Meškauskas Feb 08 '13 at 15:11
  • @Mikedg: Strangely,when I click on an HTML file (mimetype correctly recognized as text/html), the `Complete action using` does not show my (installed) Chrome. It even shows Dolphin, HTMLViewer and even Tasker, but no Chrome. Any idea why? – Luis A. Florit Nov 14 '13 at 12:58
1

Open and load as raw resource and then place into WebView with loadData:

InputStream page = getResources()
   .openRawResource(R.raw.my_web_page);
 BufferedReader r = new BufferedReader(new InputStreamReader(page));
String s;
while ((s = r.readLine()) != null)
  builder.append(s);
r.close();
myWebView.loadData(builder.toString(), "text/html", "UTF-8");

Only your own application can easily read your assets.

If you need to open the file with exactly the external browser, save the obtained string to some public location where the external browser could also access it.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
0

You need to use the Android Assetmanager to read a file in assets

AssetManager am = context.getAssets();
InputStream is = am.open("trialhtml.html");
smk
  • 5,340
  • 5
  • 27
  • 41
  • After converting it into the inputstream then how to open it in the browser. Please elaborate. –  Feb 08 '13 at 13:36
0

Try Using webView you can open it. And if you are using java script file in html then you have to add

webView.getSettings().setJavaScriptEnabled(true);

into your activity and also add javascript file into assest/www folder.

webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl("file:///android_asset/www/index.html");
Maulik
  • 3,316
  • 20
  • 31
0

To save the html string as file:

 public File saveStringToHtmlFile(String htmlString) {
        File htmlFile = null;
        try {
            // If the file does not exists, it is created.
            htmlFile = new File((ContextUtils.getAppContext()).getExternalFilesDir(null), "HTML_FILE_NAME.html");
            if (!htmlFile.exists()) {
                htmlFile.createNewFile();
            }

            // Adds a line to the file
            BufferedWriter writer = new BufferedWriter(new FileWriter(htmlFile, false));
            writer.write(htmlString);
            writer.close();
        } catch (IOException e) {
            Log.e(TAG, "Unable to write to HTML_FILE_NAME.html file.");
        }
        return htmlFile;
    }

To open the html file with browser:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(htmlFile), "text/html");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
evya
  • 3,381
  • 1
  • 25
  • 28