2013-08-25 137 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版本能夠做到這一點。

相關問題