我有一个资产文件在我的项目中的文件包含html代码现在我想得到这些代码字符串上的贝娄代码,所以请帮助我如何阅读资产文件字符串上的贝娄代码
public class StartPage {
final String FILENAME = data();
private static final String HEAD_1 = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\">"
+ "<head>"
+ "<meta content=\"en-us\" http-equiv=\"Content-Language\" />"
+ "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" />"
+ "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\">"
+ "<title>";
@NonNull
public static File getStartPageFile(@NonNull Application application) {
return new File(application.getFilesDir(), FILENAME);
}
@NonNull private final String mTitle;
@Inject Application mApp;
@Inject SearchEngineProvider mSearchEngineProvider;
public StartPage() {
BrowserApp.getAppComponent().inject(this);
mTitle = mApp.getString(R.string.home);
}
@NonNull
public Single<String> getHomepage() {
return Single.create(new SingleAction<String>() {
@Override
public void onSubscribe(@NonNull SingleSubscriber<String> subscriber) {
StringBuilder homepageBuilder = new StringBuilder(HEAD_1 + mTitle );
BaseSearchEngine currentSearchEngine = mSearchEngineProvider.getCurrentSearchEngine();
String icon = currentSearchEngine.getIconUrl();
String searchUrl = currentSearchEngine.getQueryUrl();
homepageBuilder.append(icon);
homepageBuilder.append(searchUrl);
File homepage = getStartPageFile(mApp);
FileWriter hWriter = null;
try {
//noinspection IOResourceOpenedButNotSafelyClosed
hWriter = new FileWriter(homepage, false);
hWriter.write(homepageBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
Utils.close(hWriter);
}
subscriber.onItem(Constants.FILE + homepage);
}
});
private String data(){
InputStream inputStream = getAsseet().openRawResource(R.raw.database);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
}
}
我试图在这里读取资产文件,但这里给出错误(无法解析方法getasset()),所以请帮助我编写代码谢谢osman
2条答案
按热度按时间qf9go6mv1#
请使用下面的代码。希望这能奏效!
jk9hmnmh2#
getAssets()
是类的方法Context
. 在活动中,你可以打电话getAssets()
因为它被当作this.getAssets()
,活动是Context
. 所以你可以打电话来Activty
. 因为你的类不是Context
,你不能打电话getAssets()
或者this.getAssets()
在你们班,因为没有这样的方法。所以现在你得去买点
Context
能够打电话getAssets()
. 你有一个Application mApp;
,它是Context
以及Activty
. 所以你可以打电话mApp.getAssets()