我的项目上有一个资产文件,文件包含html代码现在我想通过字符串在Bellow代码上获取这些代码所以请帮助我 我如何在Bellow代码中的字符串中读取资产文件
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())所以请通过编写代码来帮助我 谢谢 奥斯曼
答案 0 :(得分:0)
getAssets()
是类Context
的方法。在“活动”中,您只能调用getAssets()
,因为它被视为this.getAssets()
,而活动是Context
的子类。这就是为什么你可以在Activty
中调用它的原因。由于您的班级不是Context
的子类,因此您无法在班级中调用getAssets()
或this.getAssets()
,因为没有此类方法。
所以现在你必须得到一些Context
才能打电话给getAssets()
。您有Application mApp;
的实例,它是Context
的子类以及Activty
。所以你可以致电mApp.getAssets()
答案 1 :(得分:0)
请使用以下代码。希望这会奏效!
String convertedString = loadJSONFromAsset(context,"myfile.html");
Log.d("converstion" ,"converted string" + convertedString ); // it will print the entire file as string.
public String loadJSONFromAsset(Context context,String fileName) {
String json = null;
try {
InputStream is = context.getAssets().open("foldername/" + fileName);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}