天天看點

Java讀取sd卡_java – 從SD卡讀取和寫入檔案

我買了一本Mario Zechner的書(“Beginning

Android Games”).在頁149,他談到在外部存儲上儲存和打開檔案.我了解代碼,但我不明白為什麼它這樣說:

為什麼這麼說呢?我擁有Manifest中的所有權限:

package="com.amzoft.android.reference"

android:versionCode="1"

android:versionName="1.0"

android:installLocation="preferExternal">

android:label="Android Reference"

android:icon="@drawable/ic_launcher"

android:debuggable="true">

android:label="Android Reference"

android:name=".AndroidReferenceActivity"

android:screenOrientation="portrait"

android:configChanges="keyboard|keyboardHidden|orientation">

android:label="LifeCycleTest"

android:name=".LifeCycleTest"

android:configChanges="keyboard|keyboardHidden|orientation"/>

android:label="SingleTouchTest"

android:name=".SingleTouchTest"

android:configChanges="keyboard|keyboardHidden|orientation"/>

android:label="MultiTouchTest"

android:name=".MultiTouchTest"

android:configChanges="keyboard|keyboardHidden|orientation"/>

android:label="KeyTest"

android:name=".KeyTest"

android:configChanges="keyboard|keyboardHidden|orientation"/>

android:label="AccelerometerTest"

android:name=".AccelerometerTest"

android:configChanges="keyboard|keyboardHidden|orientation"/>

android:label="AssetsTest"

android:name=".AssetsTest"

android:configChanges="keyboard|keyboardHidden|orientation"/>

android:label="ExternalStorageTest"

android:name=".ExternalStorageTest"

android:configChanges="keyboard|keyboardHidden|orientation"/>

我的代碼:

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();

}

}

我希望有人可以幫助我,因為我現在很困惑.