2010-10-04 6 views
0

J'essaie d'obtenir que mon application crée un répertoire à la racine de la carte SD, si ce répertoire n'existe pas déjà. Lorsque je lance l'application, la notification de création de toasts «Création du répertoire de base ...» s'affiche, mais le répertoire n'est pas créé ... Qu'est-ce que je fais de mal ?? (Autorisations PS sont définies pour l'écriture au stockage externe)Problème Création d'un répertoire sur la carte SD

public class Main extends Activity { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

    // Check SD Card for Read/Write 

      boolean mExternalStorageWriteable = false; 
      String state = Environment.getExternalStorageState(); 

      if (Environment.MEDIA_MOUNTED.equals(state)) { 
       // We can read and write the media 
       mExternalStorageWriteable = true; 
      } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
       // We can only read the media 
       mExternalStorageWriteable = false; 
      } else { 
       // Something else is wrong. It may be one of many other states, but all we need 
       // to know is we can neither read nor write 
       mExternalStorageWriteable = false; 
      } 

      if (mExternalStorageWriteable == false) { 
      //Toast Notification that SD Card is not Accessible 
      Context context = getApplicationContext(); 
      CharSequence text = "SD Card is NOT Accessable, Please Remount and Try Again"; 
      int duration = Toast.LENGTH_SHORT; 

      Toast toast = Toast.makeText(context, text, duration); 
      toast.show(); 
      } else { 
      //Check If Home Dir Exists 

      boolean exists = (new File("/AndGuard/")).exists(); 
      if (exists) { 
       Context context = getApplicationContext(); 
       CharSequence text = "Accessing Data Files..."; 
       int duration = Toast.LENGTH_SHORT; 

       Toast toast = Toast.makeText(context, text, duration); 
       toast.show(); 

      } else { 
       Context context = getApplicationContext(); 
       CharSequence text = "Creating Home Directory..."; 
       int duration = Toast.LENGTH_SHORT; 

       Toast toast = Toast.makeText(context, text, duration); 
       toast.show(); 

      // Create a directory; all non-existent ancestor directories are 
     // automatically created 

      // Create a directory; all non-existent ancestor directories are 
     // automatically created 
     File root = Environment.getExternalStorageDirectory(); 
     boolean success = (new File(root,"directoryName")).mkdirs(); 
     if (!success) { 
      // Directory creation failed 
     } 
      }   
      } 

Voici mon fichier Manifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="soapbox.sym3try.andguard" 
     android:versionCode="1" 
     android:versionName="1.0"> 
      <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".Main" 
        android:label="@string/app_name"> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
    <uses-sdk android:minSdkVersion="4" /> 


</manifest> 

J'ai essayé cela aussi, il ne fonctionnait pas, mais il pourrait être une étape la bonne direction car elle crée le répertoire et il remplit avec un fichier de res/toutes premières en une seule étape:

public boolean saveas(int ressound){ 
     byte[] buffer=null; 
     InputStream fIn = getBaseContext().getResources().openRawResource(ressound); 
     int size=0; 

     try { 
      size = fIn.available(); 
      buffer = new byte[size]; 
      fIn.read(buffer); 
      fIn.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      return false; 
     } 

     String path="/sdcard/AndGuard/List1"; 
     String filename="hosts"; 

     boolean exists = (new File(path)).exists(); 
     if (!exists){new File(path).mkdirs();} 

     FileOutputStream save; 
     try { 
      save = new FileOutputStream(path+filename); 
      save.write(buffer); 
      save.flush(); 
      save.close(); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      return false; 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      return false; 
     } 
     return exists;  
} 
+0

Envisagez de publier votre manifeste afin que nous puissions confirmer que votre autorisation est correctement configurée. – CommonsWare

+0

Ajouté le manifeste –

Répondre

0

cela peut ne pas faire quoi que ce soit, mais juste après WRITE_EXTERNAL_STORAGE » dans votre add manifeste cette autorisation

android:name="android.permission.READ_PHONE_STATE" 
+0

je l'ai ajouté, mais rien n'a changé –

0
//create file dir 
File wallpaperDirectory = new File("/sdcard/example"); 
// have the object build the directory structure, if needed. 
wallpaperDirectory.mkdirs(); 

Cela fonctionne pour moi. Vous devrez définir

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"> 
0

Les utilisations-autorisation doit être dans la balise manifeste, et non de l'activité.

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="soapbox.sym3try.andguard" 
     android:versionCode="1" 
     android:versionName="1.0"> 

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
    <uses-sdk android:minSdkVersion="4" /> 


    <application 
     android:icon="@drawable/icon" 
     android:label="@string/app_name"> 

     <activity 
      android:name=".Main" 
      android:label="@string/app_name"> 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
</manifest> 

Vous ne pouvez pas créer des fichiers dans le répertoire racine, donc je suppose qu'il doit me

File root = Environment.getExternalStorageDirectory(); 
boolean exists = (new File(root, "AndGuard")).exists(); 

Coder en dur/sdcard/est erroné.

String path="/sdcard/AndGuard/List1" 

droit

File path = new File(root, "AndGuard/List1"); 

Et

save = new FileOutputStream(path+filename); 

vous donnera

"/sdcard/AndGuard/List1hosts" 

Il faut donc

save = new FileOutputStream(new File(path, filename)); 
Questions connexes