-6

这段代码给我带来了问题。问题是:dummyTextID

InputStream inputStream = getResources().openRawResource(dummyTextID);

我已经dummyTextID@override protected void下定义了,但是必须将读取方法放在单独的下面private String,这会创建问题

我正在提供我的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.word);

    Uri uri = getIntent().getData();
    Cursor cursor = managedQuery(uri, null, null, null, null);

    if (cursor == null) {
        finish();
    } else {
        cursor.moveToFirst();

        TextView word = (TextView) findViewById(R.id.word);
        word.setMovementMethod(new ScrollingMovementMethod());
        TextView definition = (TextView) findViewById(R.id.definition);
        definition.setMovementMethod(new ScrollingMovementMethod());

        TextView details = (TextView) findViewById(R.id.details);
        details.setText(readText());

        int wIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_WORD);
        int dIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_DEFINITION);
        int Index = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_DETAILS);
        int dummyTextID = getResources().
        getIdentifier(cursor.getString(Index), "raw", getPackageName());


        word.setText(cursor.getString(wIndex));
        definition.setText(cursor.getString(dIndex));
        definition.setText(Html.fromHtml(cursor.getString(dIndex)));
        details.setText(cursor.getString(Index));
        details.setText(Html.fromHtml(cursor.getString(Index)));
    }
}


private String readText() {
    InputStream inputStream = getResources().openRawResource(dummyTextID);


    ByteArrayOutputStream byteArrayOutputStream = new 

    ByteArrayOutputStream();

    int i;
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i= inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return byteArrayOutputStream.toString();
}

public void onBackPressed() {
    onSearchRequested();
}
4

1 回答 1

1

您应该能够使用openRawResource将二进制文件从原始资源文件夹复制到设备。

请参阅下面的 API:

InputStream ins = getResources().openRawResource(R.raw.my_db_file);

但是你正在做的是:

int dummyTextID = getResources().getIdentifier(cursor.getString(Index), "raw", getPackageName());
InputStream inputStream = getResources().openRawResource(dummyTextID);

您的虚拟文本 id 不是二进制的,它是一个 int 。将其转换为二进制,然后传递。这应该工作。

于 2013-04-24T08:21:41.533 回答