我开发了一个用于在Android中显示我的HTML页面的应用程序。我使用webview来显示我的本地html页面。它运行正常。我需要将同样的本地html文件用于Google Glass。可能吗?我在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;
}
答案 0 :(得分:1)
你可以在玻璃上使用webview,但我发现它在滚动等方面效果不佳,然后最好在浏览器活动中打开它。我有一个HTML文件作为资源,所以在浏览器中打开它之前我必须先将它复制到共享内存,这样的事情......
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);
然后copyFile方法看起来像......
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;
}
这可能不是一个完美的解决方案,但它对我有用。希望能帮助到你。