我在名为n0.html,n1.html等的资产中有html文件。我想创建包含此文件链接的listview,但我不知道该怎么做。 我有一个原始文件夹的决定。我该如何将其更改为资产文件?
public class ViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Bundle bundle = getIntent().getExtras();
String itemname = "n" + bundle.getString("defStrID"); //getting string and forming resource name
Context context = getBaseContext(); //getting context
//reading text file from resources by name
String text = readRawTextFile(context, getResources().getIdentifier(itemname, "raw", "ru.falcon5f.carguide;"));
WebView wWebView = (WebView) findViewById(R.id.webView);
String summary = "<!Doctype html><html><head><meta charset=utf-8></head><body>" + text + "</body></html>";
wWebView.loadData(summary, "text/html", "utf-8"); //uploading text to webview
}
public static String readRawTextFile(Context ctx, int resId) //reading text raw txt file
{
InputStream inputStream = ctx.getResources().openRawResource(resId);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
StringBuilder text = new StringBuilder();
try {
while (( line = buffreader.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
return null;
}
return text.toString();
}
}
加 如果我问过于愚蠢的问题并且问得太多,我很抱歉,但我想开始我的第一个申请。这对我来说非常重要。所以它包含两个活动:
ViewActivity 我已根据您的建议更改了
public class ViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Bundle bundle = getIntent().getExtras();
String htmlFileName = "n" + bundle.getString("defStrID"); //getting file name
Context context = getBaseContext(); //getting context. You still need that
//reading text file from resources by name
try {
String text = readAssetTextFile(context, htmlFileName);
WebView wWebView = (WebView) findViewById(R.id.webView);
String summary = "<!Doctype html><html><head><meta charset=utf-8></head><body>" + text + "</body></html>";
wWebView.loadData(summary, "text/html", "utf-8"); //uploading text to webview
} catch (IOException e) {
Log.e("TAG", e); // note that you will need to import android.util.Log
}
}
public static String readAssetTextFile(Context ctx, String fileName) throws IOException //reading html file from assets
{
InputStream inputStream = ctx.getAssets().open(fileName);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
StringBuilder text = new StringBuilder();
try {
while (( line = buffreader.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
return null;
}
return text.toString();
}
}
在Log.e("TAG", e);
Eclipse中想要将“e”的类型更改为“String”。
请帮帮我。如果你能在我的斗争中进一步帮助我,我将非常感激。
答案 0 :(得分:1)
它不会有那么大的不同。您现在可以访问资产中的文件:
InputStream inputStream = ctx.getAssets().open(fileName);
您可以放置此代替行InputStream inputStream = ctx.getResources().openRawResource(resId);
。那么你需要的是传递正确的文件名。使用资产时,您无需使用ID。
编辑编辑您的代码段:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Bundle bundle = getIntent().getExtras();
String htmlFileName = "n" + bundle.getString("defStrID") + ".html"; //getting file name
Context context = getBaseContext(); //getting context. You still need that
//reading text file from resources by name
try {
String text = readAssetTextFile(context, htmlFileName);
WebView wWebView = (WebView) findViewById(R.id.webView);
String summary = "<!Doctype html><html><head><meta charset=utf-8></head><body>" + text + "</body></html>";
wWebView.loadData(summary, "text/html", "utf-8"); //uploading text to webview
} catch (IOException e) {
Log.e("TAG", "Exception thrown", e); // note that you will need to import android.util.Log
}
}
public static String readAssetTextFile(Context ctx, String fileName) //reading html file from assets
{
InputStream inputStream = ctx.getAssets().open(fileName);
.....