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
我写了文件到外部存储。同样的问题! –