如何打开本地文件的方法——启动浏览器

10

我正在尝试发送意图到浏览器以打开本地文件。我希望使用默认浏览器打开此文件。

if(file.exists()){
  Log.d(TAG, "file.exists");
  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
  context.startActivity(intent);
}

但它抛出了一个异常

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

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));

当我将文件URL (file:///sdcard/release_notes.htm) 写入浏览器地址栏时,它会按预期打开。

7个回答

10

浏览器仅为HTML和其他兼容文件启动。这应该可以运行:

Intent intent = new Intent(ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/html");

6

这是我目前使用的方法。我直接从Android默认浏览器的Manifest.xml中获取了mime类型。显然,text/html的mime类型只适用于http(s)和内联方案。

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setDataAndType(Uri.fromFile(filePath), "application/x-webarchive-xml");
startActivity(intent);

不确定它是否适用于所有android/手机/浏览器组合,但这是我能让它工作的唯一方法。

编辑:使用chrome测试无法工作。也无法在我的2.3.3设备上工作。似乎可以在Android 4.0中的默认浏览器中工作。


4
你需要在意图中添加可浏览的类别。
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
intent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(intent);

5
似乎没有帮助。 08-10 14:03:48.414:ERROR/AndroidRuntime(29612):android.content.ActivityNotFoundException:找不到处理意图 {act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=file:///sdcard/release_notes.htm} 的活动。 - roose
2
也许您可以尝试将浏览器类名明确地添加到意图中:intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity"); - Zharf
我认为这不是可访问性问题,因为如果我手动将URL写入地址栏,浏览器会打开它。此外,我认为如果无法访问文件,file.exists()应该返回false。难道它不应该抛出不同的异常吗?不同设备之间可能会有不同的行为吗?我正在运行Desire HD。 - roose
1
这非常奇怪,因为添加类别正是文档所说要做的。我认为添加显式类名不是一个好的解决方案,因为用户可能已将Opera / Firefox /其他浏览器设置为其喜爱的浏览器。在这种情况下,没有理由运行Android浏览器。 - rds
对我没用。解决方案是:intent.setDataAndType(Uri.fromFile(file), "text/html"); - evya
显示剩余3条评论

3
这里提供一种更为强健的方法:

private void openInBrowser(File file) {
    final Uri uri = Uri.fromFile(file);
    try {
        final Intent browserIntent = new Intent(Intent.ACTION_VIEW);
        browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
        browserIntent.setData(uri);
        startActivity(browserIntent);
    } catch (ActivityNotFoundException e) {
        final Intent browserIntent = new Intent(Intent.ACTION_VIEW);
        browserIntent.setDataAndType(Uri.fromFile(file), "text/html");
        startActivity(browserIntent);
    }
}

我在 Nexus One (API 16, 4.1.2) 上测试过这个方法,try 是有效的;但是在 Nexus 5 (API 22, 5.1.1) 上,只有 catch 能生效。


2
也许这个可以解决问题:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/html");
startActivity(intent);

0

这段代码可在API 10和API 11上运行。

File f = new File("/mnt/sdcard/index.html");

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setDataAndType(Uri.fromFile(f), "application/x-webarchive-xml");
// Have to add this one in order to work on Target 2.3.3 (API 10)
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
startActivity(intent);

0
问题在于新的活动无法访问您应用程序内部的HTML页面,因为它是一个不同的应用程序,并且没有权限这样做。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接