本文介绍了如何启动浏览器打开本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要发送意图浏览器打开本地文件。我希望使用默认浏览器打开此文件。
I'm trying to send intent to browser to open local file. I wish to use default browser to open this file.
if(file.exists()){
Log.d(TAG, "file.exists");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
context.startActivity(intent);
}
但它引发我和exeption
But it throws me and exeption
08-10 13:27:58.993: ERROR/AndroidRuntime(28453): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///sdcard/release_notes.htm }
如果我用下面的意图浏览器中打开google.com预期
If I use following intent browser opens google.com as expected
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
此外,当我写的文件的URL 。(文件:///sdcard/release_notes.htm)
到浏览器的地址栏将打开它如预期
Also when I write the file url (file:///sdcard/release_notes.htm)
to browser address bar it opens it as expected.
推荐答案
您需要添加可浏览的类别中的意图。
You need to add browsable category in the intent.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
intent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(intent);
这篇关于如何启动浏览器打开本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!