开发者

Write to file depending on minSdkVersion - android

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 :)


You need to add 2 new permissions:

    <uses-permission 
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission 
android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

These permissions weren't added until API level 4, so anything below that gets them for free.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜