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版本能夠做到這一點。
所以我的解決方案是我必須創建第三方bowser可訪問的文件? –
@ArjunU:把它放在外部存儲器上。或者,[使用'FileProvider'](http://developer.android.com/reference/android/support/v4/content/FileProvider.html)通過'content://''Uri '。而且,無論哪種情況,您仍然需要找到兼容的瀏覽器。請注意,在撰寫本文時,Chrome不兼容,但看起來Firefox可能適用於外部存儲上的文件。 – CommonsWare
我寫了文件到外部存儲。同樣的問題! –