2011-09-28 73 views
0

我想做一个简单的操作。在android中创建一个文件,但我不知道为什么。 android没有创建这个文件。 Android在调试2和调试3之间停止。文件夹未创建。我不明白为什么。在Android中创建文件的问题

try { 
File inventoryDir = new File(Environment.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(); 
       } 

我有测试此代码

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(); 

蒙山这段代码,我没有问题的创建文件,但如果我用Windows资源管理器搜索我没有找到这个文件。在清单文件

EDITED

File inventoryDir = new File(Environment.getExternalStorageDirectory()+"/ludovic"); 
     if (!(inventoryDir.exists())){ 
     inventoryDir.mkdir(); 
     } 

也WRITE_EXTERNAL_STORAGE权限:

+0

我使用该权限 \t <用途的许可机器人:名称= “android.permission.WRITE_EXTERNAL_STORAGE”/> \t <使用的许可机器人:名称= “android.permission.ACCESS_COARSE_LOCATION”/> \t <使用说明 - 权限android:name =“android.permission.ACCESS_FINE_LOCATION”/> \t Agriesean

+0

您可以发布Logcat内容,我们可以确切知道引发了什么异常。 – Aloong

+0

这是一个警告:java.io.FileNotFoundException:/mnt/sdcard/ludovic/session.txt(权限被拒绝)。 – Agriesean

回答

0
<?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" /> 

看看这段代码示例。使用权限声明的位置是密钥。只要尝试进出活动!

+0

坦克的。我从来没有认为这是为了这个许可。 – Agriesean

0

更换

inventoryDir.mkdirs(); 

inventoryDir.mkdir(); 

最新

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(); 
} 
+0

我有类似的结果 – Agriesean

+0

看到最新的代码 – jazz

+0

类似的结果。他们之前只有这个 public class Main extends Activity { \t FileOutputStream fos; \t \t 保护无效的onCreate(捆绑savedInstanceState){ \t \t // TODO自动生成方法存根 \t \t super.onCreate(savedInstanceState); \t \t setContentView(R.layout.main); – Agriesean

0

你可以试试这个:

File inventoryDir = new File(Environment.getExternalStorageDirectory()+"/ludovic/"); 
String userfile = "session.txt"; 
+0

日志停止他们没有变化。 – Agriesean

+0

文件夹怎么样,ludovic是创建的文件夹? – Caner

+0

该文件夹未创建。 – Agriesean