0

I developed an app for displaying my HTML pages in android. I used webview to display my local html pages. It working fine.I need to use this same local html file to Google Glass. Is it possible? I used below code for android.

 File f = copyFile(R.raw.index, "index.html");
        File file = new File(f.getAbsolutePath()); 
        String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
        String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setClassName("com.google.glass.browser", "com.google.glass.browser.WebBrowserActivity");
        i.setDataAndType(Uri.fromFile(file),mimetype);
        mContext.startActivity(i);

private File copyFile(int resourceId, String filename) {
        InputStream in = null; 
        OutputStream out = null;
        File outFile = null;
        try {
            in = mContext.getResources().openRawResource(resourceId);
            outFile = new File(mContext.getExternalFilesDir(null), filename);
            Log.d("TestHTML", "output file" +  outFile.getAbsolutePath());
            out = new FileOutputStream(outFile);
            byte[] buffer = new byte[1024];
            int read;
            while((read = in.read(buffer)) != -1){
                out.write(buffer, 0, read);
            }
        } catch(IOException e) {
            Log.e("TestHTML", "Failed to copy file", e);
        } finally {
            try {
                in.close();
                out.flush();
                out.close();
                in = null;
                out = null;
            } catch (Exception e){}
        }
        return outFile;
    }

1 Answers1

1

You can use webview in glass, but I found it didn't work well with scrolling etc, and then it was better to just open it up in a browser activity. I had an HTML file as a resource so before opening it up in the browser I had to copy it to shared memory first, something like this ...

File f = copyFile(R.raw.my_html_file, "file.html");
File file = new File(f.getAbsolutePath()); 
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setClassName("com.google.glass.browser", "com.google.glass.browser.WebBrowserActivity");
i.setDataAndType(Uri.fromFile(file),mimetype);
mContext.startActivity(i);

And then the copyFile method looks like ...

private File copyFile(int resourceId, String filename) {
    InputStream in = null; 
    OutputStream out = null;
    File outFile = null;
    try {
        in = mContext.getResources().openRawResource(resourceId);
        outFile = new File(mContext.getExternalFilesDir(null), filename);
        Log.d(TAG, "output file" +  outFile.getAbsolutePath());
        out = new FileOutputStream(outFile);
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    } catch(IOException e) {
        Log.e(TAG, "Failed to copy file", e);
    } finally {
        try {
            in.close();
            out.flush();
            out.close();
            in = null;
            out = null;
        } catch (Exception e){}
    }
    return outFile;
}

This probably isn't a perfect solution, but it works for me. Hope it helps.

Ben
  • 1,767
  • 16
  • 32
  • my_html_file is an your folder name inside Raw folder? – user3602987 May 12 '14 at 09:32
  • It's the file name, so res/raw/my_html_file.html is referenced by R.raw.my_html_file – Ben May 12 '14 at 12:00
  • for the android app I'd continue using the web view as you can then put all your branding etc round it, but as there isn't really the space to do that on glass, it's easier to open up the proper glass browser activity. – Ben May 12 '14 at 12:01
  • Can you please help this...this is my first project – user3602987 May 12 '14 at 12:31
  • `file.html` is just the output name of the file, not that important, but can be the same as the original. Your code looks ok, does it work? – Ben May 12 '14 at 13:48
  • I don't have google glass...Waiting for someone help.....Does my code is correct? – user3602987 May 12 '14 at 16:13
  • You can test it on any android device if you swap out the intent for `i.setClassName("com.google.android.browser", "com.android.browser.BrowserActivity");` to use the stock android browser rather than the glass browser. – Ben May 12 '14 at 16:50
  • So many issue while keep the HTML files inside raw folder...The index.html file is not loading images from other folder and it won't displays the images in html pages. – user3602987 May 13 '14 at 06:23
  • StackOverflow is for helping with programming issues that others may also have, not for one-to-one code reviews, if you have another specific issue you can ask another question but I'm afraid I don't have the time to fix all your issues, and this isn't the site for that. Sorry. – Ben May 14 '14 at 15:48