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

时间:2013-08-25 16:59:04

标签: 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" />。但是在我点击打开后,我得到的使用应用程序选项的完成操作是Adobe Reader和UTorrent远程。浏览器未显示。我手机里有Opera和股票浏览器。我的代码出了什么问题?我使用了自定义字体

注意:

  • 我不想在我的应用中使用WebView。我想只在浏览器中打开它。
  • “getActivity()”因为此代码位于片段中。

修改

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

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

2 个答案:

答案 0 :(得分:1)

  

我的代码出了什么问题?

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

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

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

答案 1 :(得分:0)

Android的内置浏览器应用程序可以访问内部存储的私有数据区域中的文件,但要完成它需要一些工作(我花了两个坚实的时间来解决这个问题)。首先,让我们处理AndroidManifest.xml文件。靠近文件顶部(就在<application>块之前),您需要指定此权限&#34;:

  <uses-permission android:name="android.permission.WRITE_SETTINGS" />

<application>块将包含各种<activity>块。一个将是您的主要HTML编辑器安装程序代码。另一个可能是实际的HTML编辑器。另一个必须是浏览器启动器。那个人需要看起来像这样:

<activity
  android:name="com.android.browser"
  android:parentActivityName="com.myHTMLeditor.Installer"
  android:allowTaskReparenting="true"
  android:autoRemoveFromRecents="true"
  android:launchMode="standard"
  android:documentLaunchMode="intoExisting"
  android:excludeFromRecents="true"
  android:exported="false"
  android:noHistory="true"
  android:screenOrientation="portrait"
  android:stateNotNeeded="true"
  >
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.LAUNCHER" />
    <category android:name="android.intent.category.APP_BROWSER" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.OPENABLE" />
    <data
      android.scheme="file"
      android.host="com.myHTMLeditor"
      android.path="/data/data/com.myHTMLeditor/files/MainPage.html"
    />
  </intent-filter>
</activity>

您可能需要使用调试器验证android.path数据。更多信息如下。

现在对于Installer.java文件 - 仅在此示例中命名,因为&#34;安装程序&#34;在上面的android.parentActivityName中指定。该.java文件需要某些导入才能使活动与文件一起使用:

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import java.lang.String;
import java.io.File;
import java.io.InputStream;
import java.io.FileOutputStream;

安装程序的活动类的第一部分需要这样的内容:

public class Installer extends Activity
{ boolean btmp, passfail=false;   //declare various variables global to the class
  File apphome, fmd;
  InputStream inp;
  FileOutputStream fout;
  Intent Browse;
  Uri u;
  Uri.Builder b;
  String fn;
  int lng;
  byte[] buffer=new byte[1024];

实际执行Install的类函数或方法需要这样的代码:

public void doInstall()
{ apphome=getFilesDir(); //VERIFY WITH DEBUGGER (for Manifest file): /data/data/com.myHTMLeditor/files
  apphome.setReadable(true, false);  //THIS IS KEY to letting the browser access files
  apphome.setWritable(true, false);  // in your application's private directory

如果安装过程在该目录中创建了一个文件,则需要这样的内容:

  fn=apphome.getPath()+"/MainPage.html"; //create file name
  fmd=new File(fn);                      //create file object
  btmp=fmd.createNewFile();              //create actual file
  if(btmp)
  { fout=new FileOutputStream(fmd);
    while( /*you have data to put into the file, fetch some of it into the byte-buffer */ )
      fout.write(buffer, 0, lng);  //lng is number of bytes put into the buffer
    fout.close();
  }
  if(btmp) //if successfully created the file, need to make it readable by the browser, too!
  { fmd.setReadable(true, false);
    fmd.setWritable(true, true);
  }

  passfail=true; //only do this when satisfied that all files are installed properly!

现在,对于启动浏览器加载MainPage.html文件的函数/方法:

public void runBrowser(View vw)
{ if(passfail)
  { Browse = new Intent(Intent.ACTION_VIEW, Uri.parse("file://"+apphome.getPath()+"/MainPage.html"));
    Browse=Browse.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
    Browse=Browse.addCategory(Intent.CATEGORY_LAUNCHER);
    Browse=Browse.addCategory(Intent.CATEGORY_APP_BROWSER);
    Browse=Browse.addCategory(Intent.CATEGORY_DEFAULT);
    Browse=Browse.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Browse=Browse.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    Browse=Browse.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
    b=new Uri.Builder();
    b=b.encodedAuthority(apphome.getPath());
    b=b.scheme("file");
    b=b.path("/MainPage.html");
    u=b.build();
    u.getHost(); //this works to set the internal "host" property from the "authority" property
    Browse=Browse.setDataAndNormalize(u);
    startActivity(Browse);
} }

最后一点:我正在使用Android API 21进行这项工作。我没有看到早期的API版本能够做多久这件事。

相关问题