我已经买了一本Mario Zechner的书(“Beginning Android Games”)。在页149,他谈到在外部存储上保存和打开文件。我理解代码,但我不明白为什么说这个:
为什么会这样说?我拥有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"/>
我的代码:
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();
}
}
我希望有人可以帮助我,因为我现在很困惑。
答案 0 :(得分:2)
我认为其权限问题会将这两者添加到清单文件
<uses-permission android:name="android.permisson.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
让我知道结果如何
答案 1 :(得分:1)
它似乎拥有正确的权限,但您缺少结束清单标记:
</manifest>
当你在这里传递代码时,也许是一个错字。