1
我需要將對象保存到公共外部存儲器(http://developer.android.com/guide/topics/data/data-storage.html#filesExternal)請向下滾動到「保存應共享的文件」,即保存文件,以便在卸載應用程序時不會被刪除。Android - 如何序列化和去序列化公共外部存儲的對象?
保存後,我需要從保存的文件中加載對象並顯示它。只是使用int對象進行測試。請幫忙。
這是我爲保存和加載
int a = 1;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
final File file = new File (path, "int_object_saved.dat");
FileOutputStream file_output_stream = null;
ObjectOutputStream object_output_stream = null;
boolean should_keep_file = true;
try
{
file_output_stream = new FileOutputStream(file);
object_output_stream = new ObjectOutputStream(file_output_stream);
object_output_stream.writeObject(a);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
should_keep_file = false;
}
catch (IOException e)
{
e.printStackTrace();
should_keep_file = false;
}
finally
{
try
{
if(object_output_stream != null)
object_output_stream.close();
if(file_output_stream != null)
file_output_stream.close();
if(!should_keep_file)
file.delete();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
else
{
Log.d("mytag", "Sorry can't write to external media");
}
int b = 0;
try
{
FileInputStream file_input_stream;
file_input_stream = activity_parameter.openFileInput("int_object_saved.dat");
ObjectInputStream object_input_stream = new ObjectInputStream(file_input_stream);
b = (Integer) object_input_stream.readObject();
object_input_stream.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (OptionalDataException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
Log.d("mytag", Integer.toString(b));
碼這是logcat的我得到
08-02 12:39:48.382: I/ApplicationPackageManager(13109): cscCountry is not German : BTU
08-02 12:39:48.421: D/mytag(13109): my Log is working :D
08-02 12:39:48.437: W/System.err(13109): java.io.FileNotFoundException: /data/data/j.violajones.facedetect1/files/int_object_saved.dat (No such file or directory)
08-02 12:39:48.437: W/System.err(13109): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
08-02 12:39:48.437: W/System.err(13109): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
08-02 12:39:48.437: W/System.err(13109): at java.io.FileInputStream.<init>(FileInputStream.java:80)
08-02 12:39:48.437: W/System.err(13109): at android.app.ContextImpl.openFileInput(ContextImpl.java:416)
08-02 12:39:48.437: W/System.err(13109): at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
08-02 12:39:48.437: W/System.err(13109): at j.violajones.facedetect1.Detector.<init>(Detector.java:94)
08-02 12:39:48.437: W/System.err(13109): at j.violajones.facedetect1.Camera_Activty.onCreate(Camera_Activty.java:72)
08-02 12:39:48.437: W/System.err(13109): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-02 12:39:48.437: W/System.err(13109): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-02 12:39:48.437: W/System.err(13109): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-02 12:39:48.437: W/System.err(13109): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-02 12:39:48.437: W/System.err(13109): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-02 12:39:48.437: W/System.err(13109): at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 12:39:48.437: W/System.err(13109): at android.os.Looper.loop(Looper.java:130)
08-02 12:39:48.437: W/System.err(13109): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-02 12:39:48.445: W/System.err(13109): at java.lang.reflect.Method.invokeNative(Native Method)
08-02 12:39:48.445: W/System.err(13109): at java.lang.reflect.Method.invoke(Method.java:507)
08-02 12:39:48.445: W/System.err(13109): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-02 12:39:48.445: W/System.err(13109): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-02 12:39:48.445: W/System.err(13109): at dalvik.system.NativeStart.main(Native Method)
08-02 12:39:48.445: D/mytag(13109): 0
,這是我的AndroidManifest.xml文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="j.violajones.facedetect1"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Camera_Activty"
android:label="@string/title_activity_camera__activty"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
謝謝你的工作完美! – 2012-08-02 12:37:04