I have bought a book ("Beginning Android Games") by Mario Zechner. On page 149, he talks about saving and opening a file on External Storage. I understand the code, however I dont understand WHY it says this:
Why does it say this? I have all the permissions in my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amzoft.android.reference"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="preferExternal">
<application
android:label="Android Reference"
android:icon="@drawable/ic_launcher"
android:debuggable="true">
<activity
android:label="Android Reference"
android:name=".AndroidReferenceActivity"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="LifeCycleTest"
android:name=".LifeCycleTest"
android:configChanges="keyboard|keyboardHidden|orientation"/>
<activity
android:label="SingleTouchTest"
android:name=".SingleTouchTest"
android:configChanges="keyboard|keyboardHidden|orientation"/>
<activity
android:label="MultiTouchTest"
android:name=".MultiTouchTest"
android:configChanges="keyboard|keyboardHidden|orientation"/>
<activity
android:label="KeyTest"
android:name=".KeyTest"
android:configChanges="keyboard|keyboardHidden|orientation"/>
<activity
android:label="AccelerometerTest"
android:name=".AccelerometerTest"
android:configChanges="keyboard|keyboardHidden|orientation"/>
<activity
android:label="AssetsTest"
android:name=".AssetsTest"
android:configChanges="keyboard|keyboardHidden|orientation"/>
<activity
android:label="ExternalStorageTest"
android:name=".ExternalStorageTest"
android:configChanges="keyboard|keyboardHidden|orientation"/>
</application>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10"/>
<uses-permission android:name="android.permisson.WRITE_EXTERNAL_STORAGE" android:required="true"/>
<uses-permission android:name="android.permission.WAKE_LOCK" android:required="false"/>
And my code:
package com.amzoft.android.reference;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
public class ExternalStorageTest extends Activity{
@Override
public void onCreate(Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
String state = Environment.getExternalStorageState();
if(!state.equals(Environment.MEDIA_MOUNTED))
{
textView.setText("SD card is not mounted");
}else{
File externalDir = Environment.getExternalStorageDirectory();
File textFile = new File(externalDir.getAbsolutePath() + File.separator + "text.txt");
try{
writeTextFile(textFile, "This is a test >:0\nLINE BREAK");
String text = readTextFile(textFile);
textView.setText(text);
if(!textFile.delete())
{
textView.setText("Couldn't remove temporary directory, sorry mate.");
}
}catch(Exception e){
textView.setText(e.getMessage());
}
}
}
private void writeTextFile(File file, String text) throws IOException
{
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(text);
writer.close();
}
private String readTextFile(File file) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder text = new StringBuilder();
String line;
while((line = reader.readLine()) != null)
{
text.append(line);
text.append("\n");//BECAUSE ITS A LINE :D
}
reader.close();
return text.toString();
}
}
I hope someone can help me, because Im confused right now.