Write to file depending on minSdkVersion - android

Posted by Simon Rosenqvist on Stack Overflow See other posts from Stack Overflow or by Simon Rosenqvist
Published on 2011-03-17T08:07:30Z Indexed on 2011/03/17 8:09 UTC
Read the original article Hit count: 268

Filed under:
|
|

Hi,
I have written a filewriter for my android application. It is to function on a Galaxy Tab, so my minSdkVersion has to be at least 4, so it will fill the screen. I originally started out with SdkVersion = 2 and at that point my filewriter worked perfectly. Changing the SdkVersion to 4 introduced the problem. My filewriter doesn't work anymore! The application runs fine, but a file doesn't get created.

My .java file looks like this:

public class HelloAndroid extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);
    tv.setText("Hello, Android");
    setContentView(R.layout.main);

    //definerer en knap kaldet button1 og sætter en listener på denne.        
    Button button1 = (Button)findViewById(R.id.btnClickMe);
    button1.setOnClickListener(btnListener);

    //definerer en knap kaldet button2 og sætter en listener på denne. 
    Button button2 = (Button)findViewById(R.id.btnClickMe2);
    button2.setOnClickListener(btnListener2);

}


//en variabel af typen 'long' deklæres og kaldes tid1.
public long time1;


private OnClickListener btnListener = new OnClickListener()
{
    public void onClick(View v)
    {
        //Når der klikkes på button1 gemmes et tal i variablen tid1.
        time1 = System.currentTimeMillis(); 

    }
};

//en variabel af typen 'long' deklæres og kaldes tid2.
public long time2;
// en variabel af typen 'string' deklæres og kaldes tid:
public String string1 = "time:";

private OnClickListener btnListener2 = new OnClickListener()
{
    public void onClick(View v)
    {
        //Når der klikkes på button2 gemmes et tal i variablen tid2.
        time2 = System.currentTimeMillis();
        // Herefter oprettes en fil kaldet "file.txt".
        try{

                File file = new File(Environment.getExternalStorageDirectory(), "file.txt");

                file.createNewFile();

                BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));
                //string1 og tid2-tid1 skrives til filen. tid2-tid1 giver den tid der går fra der er trykket på den ene knap til den anden i millisekunder.
                writer.write(string1 + "\t" + (time2-time1));
                writer.newLine();
                writer.flush();
                writer.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

    }

};

}

And my manifest.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".HelloAndroid"
              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>

Why does my filewriter not work with minSdkVersion > 2? Do i have to make a new filewriter? or what to do?
Sorry for the messy code, i'm quite new to programming :)

© Stack Overflow or respective owner

Related posts about android

Related posts about SDK