开发者

where android saves serialized objects?

开发者 https://www.devze.com 2023-04-02 23:09 出处:网络
I\'m looking for a place where are objects after serialization ? I would like to put serialized objects(created in another app) to my android project and then only load this files in my game. The prob

I'm looking for a place where are objects after serialization ? I would like to put serialized objects(created in another app) to my android project and then only load this files in my game. The problem is that I don't know where do I have to put this objects ? and even if I save object from android app it never saves in my project folder. My load and save functions for objects

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;
 }

If I copy my object file to /levels/ and use

final Map map2 = MapManager.load(this, "/levels/map1.lvl");

or

final Map map2 = MapManager.load(this, "levels/map1.lvl");

it never works

How should I do it ?


You're storing them as app private files (Context.MODE_PRIVATE) to a folder only the app has access to, so no other application will be able to access them.

I think the easiest way to store them so they can be shared between apps is to store them to the sdcard using:

Environment.getExternalStorageDir();

Be aware to store them as globally readable.

0

精彩评论

暂无评论...
验证码 换一张
取 消