我在写入SD卡时遇到了问题,以下是代码:
(抱歉,代码的格式可能有些混乱,因为我只是复制粘贴)
我还没有实现readFromFile()。问题在于root.canWrite()始终返回false。 这是清单文件:
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。 这是清单文件:
<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 卡和手机存储,它告诉我在 SD 卡上有 1GB 可以写入。请帮忙。
谢谢 :)