android - 无法在浏览器中打开本地文件

标签 android html android-intent browser

(我已经检查过SO中的其他类似问题)

我正在尝试创建一个 HTML 编辑器。我有一个编辑文本,想在浏览器中打开其中输入的 HTML 代码。我通过将编辑文本内容复制到 .html 文件然后打开它来执行此操作。

String filename = "temp.html";
File file = new File(getActivity().getFilesDir(), filename);
FileOutputStream outputStream;
    try {
    outputStream = getActivity().openFileOutput(filename,
            Context.MODE_PRIVATE);
    outputStream.write(editText.getText().toString().getBytes());
    outputStream.close();
} catch (Exception e) {
    e.printStackTrace();
}

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
startActivity(intent);

我已经添加了 <uses-permission android:name="android.permission.INTERNET" />在 list 中。但是在我点击打开之后,我得到的使用应用程序选项完成操作是 Adob​​e Reader 和 UTorrent 远程。没有显示浏览器。我的手机里有 Opera 和股票浏览器。我的代码有什么问题?我为

使用了自定义字体

注意:

  • 我不想在我的应用程序中使用 WebView。我只想在浏览器中打开它。
  • “getActivity()”因为这段代码在 fragment 中。

编辑:

File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/temp");
        dir.mkdirs();
        File file = new File(dir, "temp.html");
        FileOutputStream outputStream;
        try {
            outputStream = new FileOutputStream(file);
            outputStream.write(et.getText().toString().getBytes());
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
        startActivity(intent);

更改代码以将文件写入外部目录。问题仍然存在。

最佳答案

What is wrong with my code?

首先,第三方应用无法访问您的文件,因为它是内部存储上的私有(private)文件。

其次,浏览器应用程序不需要支持 file:// Uri 值,甚至不需要支持 content:// Uri 值(如果您想使用它来将私有(private)文件公开给第三方应用程序)。

如果要显示本地 HTML,请使用 WebView 小部件。或者,遍历可用的 Web 浏览器应用程序名册,直到找到支持 file://content:// Uri 方案的应用程序,然后鼓励用户安装该浏览器。

关于android - 无法在浏览器中打开本地文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18431452/

相关文章:

java - 如何更新 ListView 中的记录以及android中的数据库

android - 调用 notifyAppWidgetViewDataChanged 后,不会在 RemoteViewsFactory 中调用 onDataSetChanged

css - 具有透明圆 Angular 图像的固定左右 div 的中心 div 的可变宽度

android - 在 Activity 之间传递二维对象

android - BroadcastReceiver 重复无变化

java - finish() 方法在 AsyncTask 内部不起作用

android - 如何使用 Android Webview 在后台播放 YouTube?

android - react native Android : Manifest merger failed

html - @media 中的浏览器表单宽度兼容性

html - 对该图像进行编码的最佳技术是什么