Share
Embed
android programming 2
This slides explain how to save data and files on android
Giovanni De Francesco
January 11, 2013
Transcript
10 slides
-
Shared preferences Editor edit = sp.edit(); sp.putString(“key”, ”value”); edit.commit(); sp.getString(“key”,
”default”; Android programming SharedPreferences sp= PreferenceManager. getDefaultSharedPreferences(this); -
Internal Storage //API Level 8, see developers.android.com for more. String
filename = "hello_file"; String string = "hello world!"; FileOutputStream fos = openFileOutput (filename, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close(); FileInputStream fis = openFileInput (filename, Context.MODE_PRIVATE); String s=new String(fis.read()); fos.close(); Android programming -
External Storage //API Level 8, see developers.android.com for more. String
state = Environment .getExternalStorageState(); // Managing SD state //Private storage File file = new File (getExternalFilesDir(null), ”MyFile.jpg"); //Or public one File path = Environment .getExternalStoragePublicDirectory(null); Android programming -
Using ListActivity public class MyListActivity extends ListActivity { @Override protected
void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] values = new String[]{"first", "second", "third","forth"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } } Android programming -
Using Dialogs AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) { // User clicked OK button } } }); builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } }); AlertDialog dialog = builder.create(); dialog.show(); Android programming -
What should we do today? Your login credential should
be saved in shared preferences. After the login the applications show me the list of color’s names, if I click on a color’s name a dialog will popup to ask me if I want to save that color name on my internal storage. Android programming