在设备的本地存储上创建Android保存文件?

问题描述:

我在Android上使用Libgdx进行游戏,并且我想在用户手机的本地存储上创建一个保存文件,而且我想知道如何去做这件事。我读过的一些文件如:https://developer.android.com/guide/topics/data/data-storage.html#filesInternal一直都很模糊,我不太明白发生了什么,或者可以让我的例子在我自己的游戏中运行。 Libgdx本身有这个功能吗?在设备的本地存储上创建Android保存文件?

在libgdx中用于存储我们使用“偏好”的数据。这有助于将数据存储在设备本身中。在这里我会给你一个例子来存储在deveice内存中的数据。

  myGame extends ApplicationAdapter{ 
      public static int count; 
      public static bitmapFont font; 
      public SpriteBatch batch; 
      public static Preferences prefs; 
      public void create() 
      { 
      batch=new SpriteBatch(); 
      font=new font(Gdx.files.internla("myFont.png")); 
      public static Preferences prefs; 
     // this is for setting up the storage for the current project 
      prefs = Gdx.app.getPreferences("myGame"); 
      } 
      public void incrementCount() 
      { 
      count++; 
      if(count>100){ 
     // here "countValue" is the key(which is the reference to the data) and "count" is the vlaue(the data). the value is stored in key value method. 

      prefs.putInteger("countValue", count); 

     // is used to flush the data in to the storage 
      prefs.flush(); 
      } 
      public void render() 
      { 
     // getting the stored value from the preference 
      pref.getInteger("countValue",countValue); 
      batch.begin(); 
      batch.draw(font,countValue+"stored value",0,0) 
      batch.end() 
       } 

     // this is the simple way to store the data into the device memory . it will work in both the android and the IOS 
+0

感谢您的帮助。 –

我认为你要找的是Preferences。这是为您的应用程序存储数据的一种方式,适用于iOS和Android。