2013-08-25 141 views
1

(我已经检查了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远程。浏览器没有显示。我的手机中有歌剧和股票浏览器。我的代码有什么问题?我已经使用了自定义字体为

注:

  • 我不希望在我的应用程序网页视图。我只想在浏览器中打开它。
  • “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); 

改变的代码写入文件到外部目录。问题依然存在。

回答

1

我的代码有什么问题?

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

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

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

+0

所以我的解决方案是我必须创建第三方bowser可访问的文件? –

+1

@ArjunU:把它放在外部存储器上。或者,[使用'FileProvider'](http://developer.android.com/reference/android/support/v4/content/FileProvider.html)通过'content://''Uri '。而且,无论哪种情况,您仍然需要找到兼容的浏览器。请注意,在撰写本文时,Chrome不兼容,但看起来Firefox可能适用于外部存储上的文件。 – CommonsWare

+0

我写了文件到外部存储。同样的问题! –

0

Android的内置浏览器应用程序可以访问内部存储器的私有数据区域中的文件,但需要一些工作才能完成它(我花了两周的时间来弄清楚这一点)。首先,我们来处理AndroidManifest.xml文件。接近文件的顶部(只是<application>块之前),你需要指定该“许可”:

<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文件 - 由于在上面的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]; 

类功能或方法实际上并安装需要像这样的代码:

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版本能够做到这一点。

相关问题