编辑:问题解决了,FileOutputStream中uncorrectly定义,更改为:
Problem solved, FileOutputStream defined uncorrectly, change to:
FOS =新的FileOutputStream(根+/+ saveFileName);
fos = new FileOutputStream( root + "/" + saveFileName );
您好,我有一个问题写入SD卡,这里是code: (很抱歉的code中的布局,只需复制pased吧)
Hello, I have a problem writing to the SD card, here is the code: (Sorry about the layout of the code, just copy pased it )
public class SaveAndReadManager {
private String result;
private String saveFileName = "eventlist_savefile";
public String writeToFile( ArrayList<Event> arrlEvents ){
FileOutputStream fos = null;
ObjectOutputStream out = null;
try{
File root = Environment.getExternalStorageDirectory();
if( root.canWrite() ){
fos = new FileOutputStream( saveFileName );
out = new ObjectOutputStream( fos );
out.writeObject( arrlEvents );
result = "File written";
out.close();
}else{
result = "file cant write";
}
}catch( IOException e ){
e.printStackTrace();
result = "file not written";
}
return result;
}
public boolean readFromFile(){
return false;
}
}
我还没有实现的readFromFile()呢。 问题是,root.canWrite()返回false,所有的时间。 这里是清单文件:
I have not implemented the readFromFile() yet. The problem is that root.canWrite() returns false all the time. Here is the manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".InfoScreen"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity android:name=".EventCalendar"
android:label="@string/app_name" />
<activity android:name=".MakeEvent"
android:label="@string/app_name" />
<activity android:name=".ViewEvent"
android:label="@string/app_name" />
</application>
<uses-sdk android:minSdkVersion="8" />
我问的写权限,并在我的AVD,如果我去设置 - > SD卡和手机存储,它告诉我,我有1 GB的SD卡上写上。请大家帮帮忙。
I've asked for the permission to write, and on my avd, if i go to settings -> SD card and phone storage, it tells me i have 1 gb on the sd card to write on. Please help.
感谢:)
而导致你看到的将writeToFile retunred? 文件没有写或档案着写?
Which result are you seeing retunred from writeToFile? "file not written" or "file cant write"?
当我跑到你的code则下降到与文件没有写的结果抓IOException异常块。这样做的原因是,果寡糖的定义不正确:
When I ran your code it dropped into the catch IOException block with the result of "file not written". The reason for this was that fos was defined incorrectly:
fos = new FileOutputStream( saveFileName );
应该是:
fos = new FileOutputStream( root + "/" saveFileName );
在我改变了这一行,我接到了将writeToFile文件写入返回的结果。
After I changed this line, I got the result "File written" returned from writeToFile.