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;
}
Envisagez de publier votre manifeste afin que nous puissions confirmer que votre autorisation est correctement configurée. – CommonsWare
Ajouté le manifeste –