LibGDX - 在 Android 设备中找不到首选项位置

LibGDX - Cannot find Preferences location in Android Device

我正在使用 LibGDX 学习偏好。我可以在 Windows 和 Android 设备中成功地 运行 以下代码。文件 "MyDemo" 存储在 我的C:盘用户目录。它是完美的。我也可以 运行 Android 设备中的代码成功。但是,我找不到文件 "MyDemo"。空无一物 在 Android/data/...

package com.hkprogram.mydemo;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class MyDemo implements ApplicationListener {

    private SpriteBatch batch;
    private BitmapFont font1;
    int screenWidth, screenHeight;

    public Preferences prefs;
    String name;

    @Override
    public void create() {

        batch = new SpriteBatch();    
        font1 = new BitmapFont();
        font1.setColor(Color.BLACK);
        font1.setScale(5);
        screenWidth=Gdx.graphics.getWidth();
        screenHeight=Gdx.graphics.getHeight();

        Preferences prefs = Gdx.app.getPreferences("MyDemo");
        prefs.putString("Name", "Peter");
        prefs.flush();

        prefs = Gdx.app.getPreferences("MyDemo");
        name=prefs.getString("Name","no name stored");
        System.out.println("Name="+name);       
    }   

    @Override
    public void dispose() {
    }
    @Override
    public void pause() {       
    }
    @Override
    public void render() {
         Gdx.gl.glClearColor(159/255.0f,220/255.0f,235/255.0f,0xff/255.0f);
         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);       
         batch.begin();
         font1.draw(batch, name, screenWidth/2, screenHeight/2);
         batch.end();
    }
    @Override
    public void resize(int arg0, int arg1) {        
    }
    @Override
    public void resume() {      
    }
}

您可以获得 MyDemo 执行此操作的路径:

File MyDemoFile = getDatabasePath("MyDemo.txt"); //choose your extension
if (MyDemoFile != null){
Log.d("Absolute path : ", MyDemoFile.getAbsolutePath());
}