Problems with creating a file in android
i would like to do a simple operation. Create a file in android, but i don't know why. android didn't create this file. Android stop between debug 2 and debug 3.The folder is not created. I don't understand why.
try {
File inventoryDir = new File(Enviro开发者_高级运维nment.getExternalStorageDirectory()+"/ludovic");
String userfile = "/session.txt";
if (!inventoryDir.exists()){
inventoryDir.mkdirs();
Log.i("debug","debug 1");
}
File userFile = new File(inventoryDir, userfile);
Log.i("debug","file path "+userFile.getAbsolutePath());
Log.i("debug","debug 2");
FileWriter fw = new FileWriter(userFile);
Log.i("debug","debug 3");
BufferedWriter bw = new BufferedWriter(fw);
Log.i("debug","debug 4");
bw.write("teste d'ecriture");
Log.i("debug","debug 5");
bw.close();
Log.i("debug","debug 6");
Log.i("debug","enregistre");
} catch (IOException e) {
Log.i("debug","non enregistre");
e.printStackTrace();
}
i have test this code
try { // catches IOException below
final String TESTSTRING = new String("Hello Android");
// ##### Write a file to the disk #####
/* We have to use the openFileOutput()-method
* the ActivityContext provides, to
* protect your file from others and
* This is done for security-reasons.
* We chose MODE_WORLD_READABLE, because
* we have nothing to hide in our file */
FileOutputStream fOut = openFileOutput("samplefile.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
osw.write(TESTSTRING);
/* ensure that everything is
* really written out and close */
osw.flush();
osw.close();
Whith this code, i didn't have problem's to create file but i don't find this file if i search with windows explorer.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="byd.eagle"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".EagleBackup"
android:label="@string/app_name">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
Look at this code sample. The location where the uses-permission declared is the key. Just try both in and out the activity!
Can you try this:
File inventoryDir = new File(Environment.getExternalStorageDirectory()+"/ludovic/");
String userfile = "session.txt";
replace
inventoryDir.mkdirs();
to
inventoryDir.mkdir();
Latest:
File inventoryDir = new File(Environment.getExternalStorageDirectory()+"/ludovic");
if (!(inventoryDir.exists())){
inventoryDir.mkdir();
}
and also WRITE_EXTERNAL_STORAGE permission in Manifest file
EDITED:
File inventoryDir = new File(Environment.getExternalStorageDirectory()+"/ludovic/");
String userfile = "session.txt";
if (!(inventoryDir.exists())){
inventoryDir.mkdir();
Log.i("debug","debug 1");
}
File userFile = new File(inventoryDir, userfile);
try {
PrintWriter out = new PrintWriter(mFile);
} catch (IOException e){
e.printStackTrace();
}
out.print("some text");
out.close();
}
精彩评论