2015-11-02 6 views
-1

Я новичок в разработке android. Могу ли я добавить файл .txt в свое приложение и использовать его для отображения текста (текстовый файл содержит абзац описания) в моей деятельности вместо того, чтобы писать все описание в файле strings.XML. Если да, пожалуйста, помогите мне с кодом. БлагодаряТекстовый файл (.txt) в android

+1

Попробуйте следующее: http://stackoverflow.com/a/8695590/3110234 – activesince93

+0

Возможный дубликат [Как я могу прочитать .txt и отобразить его как TextView в Android?] (Http://stackoverflow.com/questions/ 8695361/how-can-i-read-txt-and-display-it-as-textview-in-android) –

+0

@ activesince93 Я хочу добавить .txt в свой проект, как в активах или исходной папке, а не с SD-карты –

ответ

0

There are a few decent answers to this in this question

//Find the directory for the SD Card using the API 
//*Don't* hardcode "/sdcard" 
File sdcard = Environment.getExternalStorageDirectory(); 

//Get the text file 
File file = new File(sdcard,"file.txt"); 

//Read text from file 
StringBuilder text = new StringBuilder(); 

try { 
    BufferedReader br = new BufferedReader(new FileReader(file)); 
    String line; 

    while ((line = br.readLine()) != null) { 
     text.append(line); 
     text.append('\n'); 
    } 
    br.close(); 
} 
catch (IOException e) { 
    //You'll need to add proper error handling here 
} 

//Find the view by its id 
TextView tv = (TextView)findViewById(R.id.text_view); 

//Set the text 
tv.setText(text.toString()); 

Если это не то, что зависит от чтения от возможного изменения .txt файла, вы должны обязательно попробовать использовать файл строки XML.

Смежные вопросы