0
我正在尋找序列化後有對象的地方?我想將序列化的對象(在另一個應用程序中創建)放到我的android項目中,然後只在我的遊戲中加載這些文件。問題是我不知道我必須把這個對象放在哪裏?即使我從android應用程序保存對象它永遠不會保存在我的項目文件夾。我的加載和保存功能對象哪裏的android保存序列化的對象?
public static void save(Context context, Map obj, String nazwa)
{
FileOutputStream str = null;
ObjectOutputStream objStr = null;
try {
str = context.openFileOutput(nazwa, Context.MODE_PRIVATE);
objStr = new ObjectOutputStream(str);
objStr.writeObject(obj);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (objStr != null) objStr.close();
} catch (IOException e) {}
try
{
if (str != null) str.close();
} catch (IOException e) {}
}
}
public static Map load(Context context, String nameFile)
{
Map obj = null;
FileInputStream str_w = null;
ObjectInputStream obj_w = null;
try
{
str_w = context.openFileInput(nameFile);
obj_w = new ObjectInputStream(str_w);
obj = (Map) obj_w.readObject();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} finally
{
try
{
if (obj_w != null) obj_w.close();
} catch (IOException e) {}
try
{
if (str_w != null) str_w.close();
} catch (IOException e) {}
}
return obj;
}
如果我將目標文件拷貝到/水平/和使用
final Map map2 = MapManager.load(this, "/levels/map1.lvl");
或
final Map map2 = MapManager.load(this, "levels/map1.lvl");
它永遠不會奏效
應該如何我這麼做?
好的,但我想在我的桌面應用程序中創建這個對象,然後只將它們複製到我的Android應用程序。我知道我可以創建對象並將它們保存在SD卡上,但我想在我的應用程序中創建文件夾(在安裝之前)放置對象,安裝時將此文件夾移動到SD卡,然後在遊戲中使用它。 – Greg
我在這裏找到了解決方案http://developer.android.com/guide/topics/data/data-storage.html#filesInternal – Greg