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
由于上面指定了“安装程序”,因此在此示例中的文件 --only 命名为此处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 版本可以做到这一点。