String LoadCity (int ID) {
//Here the magic starts: we import text file that we need
int resId = getResources().getIdentifier("c" + Integer.toString(ID), "raw", getPackageName());
InputStream is = getResources().openRawResource(resId);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader buffreader = new BufferedReader(isr);
String line;
StringBuilder content = new StringBuilder();
try {
while ((line = buffreader.readLine()) != null) {
content.append(line);
content.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
String toSplit = content.toString();
return toSplit;
}
错误
at com.superprower.aroundtheworld.gameactivity.LoadCity(gameactivity.java:88)
点数:
int resId = getResources()。getIdentifier(“c”+ Integer.toString(ID),“raw”,getPackageName()); InputStream是= getResources()。openRawResource(resId);
我想导入文件cN,其中N - 是ID,将其转换为字符串并返回。文件c1 / c2 / c3 / c4存在。他们不是TXT。似乎我得到了一些错误的解决方案。我的错误在哪里?
答案 0 :(得分:0)
试试这个
String uri = "raw/"+"c"+ID;
// int res = R.raw.c;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
答案 1 :(得分:0)
也许你需要一些像这样的代码:
private final int getIdByNames(Context ctx, String resType, String fileName) {
if (ctx == null || resType == null || fileName == null || "".equals(resType) || "".equals(fileName)) {
return 0;
}
int id = ctx.getResources().getIdentifier(fileName, resType, ctx.getPackageName());
return id;
}
这些:
public final int getRawIdByName(Context ctx, String fileName) {
return getIdByNames(ctx, "raw", fileName);
}
然后,也许你需要打电话:
int resId = ResUtil.getRawIdByName(yourContext, "c1");
InputStream is = getResources().openRawResource(resId);