开发者

How to launch browser to open local file

开发者 https://www.devze.com 2023-03-27 04:52 出处:网络
I\'m trying to send intent to browser to open local file. I wish to use default browser to open this file.

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 i开发者_JAVA百科ntent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
  context.startActivity(intent);
}

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 }

If I use following intent browser opens google.com as expected

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

Also when I write the file url (file:///sdcard/release_notes.htm) to browser address bar it opens it as expected.


The browser is started only for HTML and other compatible files. this should work:

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


This is what's working for me. I took the mime type directly from the Manifest.xml of the Android's default browser. Apparently the text/html mime is only for http(s) and inline schemes.

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

Not sure if it works in every android/phone/browser combination but it's the only way I could get it to work.

Edit: Tested with chrome and doesn't work. Also doesn't work with my 2.3.3 device. Seems to work with the default browser in Android 4.0.


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);


Here's a slighly more robust approach:

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);
    }
}

I've tested this on a Nexus One (API 16, 4.1.2), where the try works, and a Nexus 5 (API 22, 5.1.1), where only the catch works.


Maybe this works:

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


The problem is that the new activity has no access to the html page inside your app since it is a different app and it has no permissions to do so.


This code works on both API 10 and 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

精彩评论

暂无评论...
验证码 换一张
取 消