2013-04-24 20 views
-6

這段代碼給我造成了問題。問題是:dummyTextID如何解決私人字符串readText()問題?

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

我下@覆蓋保護無效定義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(); 
} 
+0

dummyTextID做什麼?它是否會返回一個文件名? – 2013-04-24 08:10:52

+0

@黑暗騎士:是的! – Tanzil 2013-04-24 08:34:47

+0

請參閱下面的我的答案並相應地做出更改。 – 2013-04-24 08:43:59

回答

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。將其轉換爲二進制,然後傳遞它。這應該工作。