0

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

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

有什么想法有什么问题吗?

这是我的清单文件

<?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;

     }
4

1 回答 1

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(); 
于 2013-04-21T07:12:24.490 回答