The app that I'm developing has to read a TXT file from an SD card storage and display it in textview.
The process to get your file (any file) onto the emulator's pretend SD card is kind of tricky. First you have to create the SD card. I did this in eclipse by opening the AVD manager, selecting the device you want to create the SD card in and clicking edit.
In the edit screen, find the SD Card section and give your SD card a side. I gave mine 4 GB.
Then you have to move your file onto the SD card. In eclipse, this can be done by running your app and opening the DDMS view. Go to the File Explorer tab. Expand the "mnt" folder. Select the "sdcard" folder. Once selected, select the button in the upper right that has an image on a phone and an arrow. This should read "Push a file onto the device" when you hover over it. Click it. Find the file on your hard drive and push that baby onto the phone. Whew. Now its available to be read by our app.
Back to java. I pulled this code from someone's awesome answer on StackOverflow but forgot to bookmark it. Sorry!
// 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, "filename.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');
}
} catch (IOException e) {
// You'll need to add proper error handling here
}
DialogDetails.setText(
text );
No comments:
Post a Comment