2010-05-24 16 views
7

Utilizzando lo data-storage page in the docs, ho provato a memorizzare alcuni dati nella scheda SD. Questo è il mio codice:Memorizzazione di dati su scheda SD in Android

// Path to write files to 
    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
        "/Android/data/"+ctxt.getString(R.string.package_name)+"/files/"; 
    String fname = "mytest.txt"; 

    // Current state of the external media 
    String extState = Environment.getExternalStorageState(); 

    // External media can be written onto 
    if (extState.equals(Environment.MEDIA_MOUNTED)) 
    { 
     try { 
      // Make sure the path exists 
      boolean exists = (new File(path)).exists(); 
      if (!exists){ new File(path).mkdirs(); } 

      // Open output stream 
      FileOutputStream fOut = new FileOutputStream(path + fname); 

      fOut.write("Test".getBytes()); 

      // Close output stream 
      fOut.flush(); 
      fOut.close(); 

     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 

Quando creo la nuova FileOutputStream ottengo un'eccezione FileNotFound. Ho anche notato che "mkdirs()" non sembra creare la directory.

Qualcuno può dirmi cosa sto facendo male?

Sto provando su un AVD con una scheda SD da 2 GB e "hw.sdCard: sì", l'Esplora file di DDMS in Eclipse mi dice che l'unica directory sulla sdcard è "LOST.DIR".

risposta

2

Prima di leggere o scrivere su scheda SD, non dimenticate di controllare la scheda SD è montata o no?

Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) 
Problemi correlati