Question

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;
    }
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top