Android also allows raw files in addition to arbitrary XML files. These raw resources, placed in /res/raw, are raw file resources such as audio, video, or text files that require localization or references through resource IDs.
String getStringFromRawFile(Activity activity)
throws IOException {
Resources r = activity.getResources();
InputStream is = r.openRawResource(R.raw.test);
String myText = convertStreamToString(is);
is.close();
return myText;
}
String convertStreamToString(InputStream is)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = is.read();
while (i != -1) {
baos.write(i);
i = is.read();
}
return baos.toString()
}
沒有留言:
張貼留言