Read raw resource of text

Read raw resource of text

Create a raw resource of text, /res/raw/mytext.txt.
Hello!

It's a example of raw text in
/res/raw/mytext.txt

Some text here:
Android is an operating system for mobile devices such as smartphones and tablet computers. It is developed by the Open Handset Alliance led by Google.


Java main code:
package com.AndroidTextResource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidTextResourceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);

TextView myTextView = new TextView(this);
setContentView(myTextView);


InputStream inputStream = getResources().openRawResource(R.raw.mytext);

ByteArrayOutputStream byteArrayOutputStream
= new ByteArrayOutputStream();

String myText = "";
int in;
try {
in = inputStream.read();
while (in != -1)
{
byteArrayOutputStream.write(in);
in = inputStream.read();
}
inputStream.close();
myText = byteArrayOutputStream.toString();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

myTextView.setText(myText);
}
}


- Read raw text file, and display in ScrollView.

0 Response to "Read raw resource of text"

Posting Komentar