Unit V-QB
Unit V-QB
PART A
3. Give the code to store the string “kongu” in your internal storage.
String str = textBox.getText().toString();
FileOutputStream fOut = openFileOutput("textfile.txt",
MODE_PRIVATE); OutputStreamWriter osw = new
OutputStreamWriter(fOut); //---write the string to the file---
osw.write(str);
osw.flush();
osw.close();
4. Give the code to read the string from the internal storage of the device.
FileInputStream fIn = openFileInput("textfile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[READ_BLOCK_SIZE];
String s = ""; int charRead;
while ((charRead = isr.read(inputBuffer)) > 0)
{ //---convert the chars to a String---
String readString = String.copyValueOf(inputBuffer, 0, charRead);
s += readString;
inputBuffer = new char[READ_BLOCK_SIZE]; } //---set the EditText to the
text that has been // read---
textBox.setText(s).
Note: To save text into a file, you use the FileOutputStream class. The
openFileOutput() method opens a named file for writing, with the mode
specified. In this example, you use the MODE_PRIVATE constant to indicate
that the file is readable by all other applications: FileOutputStream fOut =
openFileOutput("textfile.txt", MODE_PRIVATE); To convert a character
stream into a byte stream, you use an instance of the OutputStreamWriter
class, by passing it an instance of the FileOutputStream object
OutputStreamWriter osw = new OutputStreamWriter(fOut);
5. Difference between flush() and close().
To ensure that all the bytes are written to the file, use the flush()
method. The close() method to close the file.
6. Name the classes used for reading the content from a file.
FileInputStream fIn = openFileInput("textfile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
7. What is the use of getExternalStorageDirectory()?
Return the full path to the external storage. Typically, it should return the “/sdcard”
path for a real device, and “/mnt/sdcard” for an Android emulator.
PART B:
1. Create a context menu for a ListView that allows users to delete or edit an item.
How would you implement this in your activity?
3. Implement a context menu for an image gallery app, allowing users to “Share”,
“Delete”, or “Set as Wallpaper” when an image is long-pressed.
4. Create a GridView that displays a collection of images from the drawable folder.
How would you implement the adapter to populate the GridView?