Upgrade to Pro — share decks privately, control downloads, hide ads and more …

android programming 2

android programming 2

This slides explain how to save data and files on android

Giovanni De Francesco

January 11, 2013
Tweet

More Decks by Giovanni De Francesco

Other Decks in Programming

Transcript

  1. Shared preferences Editor edit = sp.edit(); sp.putString(“key”, ”value”); edit.commit(); sp.getString(“key”,

    ”default”; Android programming SharedPreferences sp= PreferenceManager. getDefaultSharedPreferences(this);
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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