【问题标题】:How to solve private String readText() issue?如何解决私有 String readText() 问题?
【发布时间】:2013-04-24 08:05:36
【问题描述】:

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

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

我在 @override protected void 下定义了 dummyTextID,但必须将读取方法放在单独的 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();
}

【问题讨论】:

  • dummyTextID 有什么作用?它会返回一个文件名吗?
  • @黑暗骑士:是的!
  • 在下面查看我的答案并进行相应的更改。

标签: java android sqlite


【解决方案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 。将其转换为二进制,然后传递。这应该可以。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 2016-11-15
    • 2011-01-04
    • 2012-03-22
    • 2021-12-22
    • 2017-10-04
    • 2020-08-05
    相关资源
    最近更新 更多