文件在外部存储中创建,但为空

时间:2013-04-20 17:43:59

标签: android

我的部分应用程序在外部存储中创建了一些文件并写入其中。

一切都像魅力一样,直到我改变了一些东西,我不记得是什么。现在应用程序创建文件但不写任何内容,即文件为空。

任何想法是什么问题?

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myloader"
    android:versionCode="26"
    android:versionName="1.1" >

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
    <uses-feature android:glEsVersion="0x00020000" android:required="true" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />



    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
         android:debuggable="true">
        <activity
            android:name="com.example.myloader.MainActivity"
            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>

这是我写入文件的地方。我没有改变任何东西。它正在工作时就像这样

public class GraphHelper {
    File directory;
    File myExternalFile;
    PrintWriter writer;
    FileOutputStream outputStream;
    ArrayList<float[]> dataList;
    float []toWrite;
    String strToWrite;
    private String filepath = "MyFileStorage";

public GraphHelper(Context context)
{
      directory = context.getExternalFilesDir(filepath);
}

public GraphHelper(Application application)
{
   directory = application.getExternalFilesDir(filepath);
}



public void writeToFile(String filename,ArrayList <float[]>data)
{
    if(isExternalStorageAvailable()&&!isExternalStorageReadOnly())
    {
        myExternalFile = new File(directory, filename);
     try {
        outputStream = new FileOutputStream(myExternalFile);
        writer = new PrintWriter(outputStream);
        dataList = data;

        for(int i=0;i< data.size();i++)
        {
            toWrite = data.get(i);
            System.out.println("toWrite[1]: "+toWrite[0]+"toWrite[2]: "+toWrite[1]);

         }
        writer.close();
        outputStream.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }}

}
private static boolean isExternalStorageReadOnly() {  
      String extStorageState = Environment.getExternalStorageState();  
      if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {  

       return true;  
      }  
      return false;  
     }  

     private static boolean isExternalStorageAvailable() {  
      String extStorageState = Environment.getExternalStorageState(); 

      if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {  
           return true;  
      }  
     return false;

     }

1 个答案:

答案 0 :(得分:1)

你永远不会打电话给print

for(int i=0;i< data.size();i++) {
    toWrite = data.get(i);
    System.out.println("toWrite[1]: "+toWrite[0]+"toWrite[2]: "+toWrite[1]);
    writer.print(toWrite);
}

writer.flush();
writer.close();