0
我對Android SDK非常陌生,無法讀寫文件。請注意,我對Java也很陌生。該程序應該簡單地將一個字符串寫入一個文件,讀取相同的字符串,然後將TextView更改爲該讀取的字符串。程序運行但不改變TextView。有人可以告訴我我做錯了什麼嗎?謝謝。Android SDK - 無法讀/寫文件
MainActivity.java
package com.example.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
public String readFileLine(String filepath) throws java.io.FileNotFoundException, java.io.IOException {
//opens file and returns first line
FileReader fr = new FileReader (new File(filepath));
BufferedReader inputStream = new BufferedReader (fr);
String temp = inputStream.readLine();
inputStream.close();
return temp;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FileOutputStream outputStream_local;
try {
outputStream_local = openFileOutput("local.txt", Context.MODE_PRIVATE);
outputStream_local.write("asdf".getBytes());
outputStream_local.close();
} catch (Exception e) {
e.printStackTrace();
}
TextView temp = (TextView) findViewById(R.id.getMe);
try {
temp.setText(readFileLine(getFilesDir().toString() + "local.txt"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/getMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
logcat的輸出
[2014-02-08 11:18:11 - Test] ------------------------------
[2014-02-08 11:18:11 - Test] Android Launch!
[2014-02-08 11:18:11 - Test] adb is running normally.
[2014-02-08 11:18:11 - Test] Performing com.example.test.MainActivity activity launch
[2014-02-08 11:18:11 - Test] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2014-02-08 11:18:14 - Test] Uploading Test.apk onto device '014FD8210301B01A'
[2014-02-08 11:18:14 - Test] Installing Test.apk...
[2014-02-08 11:18:18 - Test] Success!
[2014-02-08 11:18:18 - Test] Starting activity com.example.test.MainActivity on device 014FD8210301B01A
[2014-02-08 11:18:18 - Test] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.test/.MainActivity }
[2014-02-08 11:18:38 - Test] ------------------------------
[2014-02-08 11:18:38 - Test] Android Launch!
[2014-02-08 11:18:38 - Test] adb is running normally.
[2014-02-08 11:18:38 - Test] Performing com.example.test.MainActivity activity launch
[2014-02-08 11:18:38 - Test] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2014-02-08 11:18:41 - Test] Application already deployed. No need to reinstall.
[2014-02-08 11:18:41 - Test] Starting activity com.example.test.MainActivity on device 014FD8210301B01A
[2014-02-08 11:18:41 - Test] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.test/.MainActivity }
[2014-02-08 11:18:41 - Test] ActivityManager: Warning: Activity not started, its current task has been brought to the front
請確保您在AndroidManifest.xml中請求寫入權限: –
發佈您的logcat。 – Kunu
@Mike M.否,不要請求該權限,因爲涉及的文件不在外部存儲器上。 –