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

时间:2015-07-13 01:45:50

标签: android libgdx sharedpreferences preferences file-handling

我正在使用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() {      
    }
}

1 个答案:

答案 0 :(得分:0)

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

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