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 );
Friday, January 20, 2012
Friday, January 13, 2012
How to View Your Android App Database: SQLite Database Broswer
I've been using SQLite Database Browser to look through my android database and make changes. Linky link:
http://sourceforge.net/projects/sqlitebrowser/
You can pull your application's database off the emulator as a .sql file and open it up in this browser to see all that fancy sql stuff.
To get your database off the emulator:
First, you have to have eclipse open and running, and run your application in your emulator of choice.
Once the app is up and running, go back to eclipse and select Window > Open Perspective > DDMS
Under the Devices tab: Make sure the emulator that is running your app is highlighted/selected. Navigate to the "File Explorer" tab. (If it's not available, select Window > Show View > File Explorer.)
In the File Explorer tab: (1) open the "data" folder, (2) open a second "data" folder, (3) find and open the folder with "your application's name", (4) find and open the "databases" folder, (5) select the file with your application's name
In the upper right of the File Explorer frame, there is a button with an image of a floppy disk with an arrow. Click on the one that says "Pull a file from device" and save your database file somewhere memorable. It might automatically save it to your project's location on your hard drive.
Now you can view it and play around with it in SQLite Database Browser :)
Keep in mind that you have to upload the new database onto the phone or emulator before the changes can be accessed by your app.
Subscribe to:
Posts (Atom)