在访问Android的错误原始资源 [英] Accessing wrong raw resource in Android

查看:192
本文介绍了在访问Android的错误原始资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code。我的想法是在OnCreate中,我就可以在/ RES一个文本文件/生到我的数据库填充一些类别。

首先一个由记号化线(myCatToken)读取文件,然后每个I再次分裂,以获得ID和名称。

由于某些原因,而不是阅读rat.txt我得到一个完全不同的文件,我不知道为什么。这实际上是读取/ RES /原料目录确实存在的文件,但它的命名一样。

看来,它是与它发送到readTxtFromRaw资源,但我看不出什么不妥的地方。

感谢

  StringTokenizer的myCatToken =新的StringTokenizer(新的String(readTxtFromRaw(R.raw.rat)));
而(myCatToken.hasMoreTokens())
{
    StringTokenizer的myCatDataToken =新的StringTokenizer(myCatToken.nextToken(),,);
    字符串insertString =新的String(插入+ DATABASE_TABLE_CATEGORIES +
            (+ KEY_CATEGORIES_CATID +,+ KEY_CATEGORIES_NAME +)值+
            (+ myCatDataToken.nextToken(+),+ myCatDataToken.nextToken()+'));
    db.execSQL(insertString);
}

有关参考包括我这个方法我使用。 mCtx是上下文:

 私人字符串readTxtFromRaw(整数rawResource)抛出IOException异常
{
    为InputStream的InputStream = mCtx.getResources()openRawResource(rawResource)。
    ByteArrayOutputStream byteArrayOutputStream =新ByteArrayOutputStream();    INT I = inputStream.read();
    而(ⅰ!= -1)
    {
        byteArrayOutputStream.write(ⅰ);
        I = inputStream.read();
    }
    inputStream.close();    返回byteArrayOutputStream.toString();
}


解决方案

其实我的Eclipse关闭并重新打开它,它工作正常。啧!

I have the following code. My idea is in OnCreate, I'll populate some categories from a text file in /res/raw to my database.

First a tokenize the read file by lines (myCatToken), then each of these I split again to get the id and name.

For some reason, instead of reading rat.txt I'm getting a totally different file and I have no idea why. The file that is actually read does exist in the /res/raw folder, however its named different.

It seems that it has something to do with the resource it sends to readTxtFromRaw, however I don't see whats wrong with it.

Thanks

StringTokenizer myCatToken = new StringTokenizer(new String(readTxtFromRaw(R.raw.rat)));
while(myCatToken.hasMoreTokens())
{
    StringTokenizer myCatDataToken = new StringTokenizer(myCatToken.nextToken(), ",");
    String insertString = new String("insert into " + DATABASE_TABLE_CATEGORIES +
            " (" + KEY_CATEGORIES_CATID + ", " + KEY_CATEGORIES_NAME + ") values " + 
            " (" + myCatDataToken.nextToken() + ", '" + myCatDataToken.nextToken() + "')");
    db.execSQL(insertString);
}

For reference I include this method I'm using. mCtx is Context:

private String readTxtFromRaw(Integer rawResource) throws IOException
{
    InputStream inputStream = mCtx.getResources().openRawResource(rawResource);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i = inputStream.read();
    while (i != -1)
    {
        byteArrayOutputStream.write(i);
        i = inputStream.read();
    }
    inputStream.close();

    return byteArrayOutputStream.toString();
}

解决方案

Actually I closed Eclipse and reopened it and it works ok. Sheesh!

这篇关于在访问Android的错误原始资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆