-1

I am learning preferences using LibGDX. I can run the following code sucessfully both in Windows and Android Device. The file "MyDemo" is stored in my C: drive user directory. It is prefect. I can run the code in Android device sucessfully as well. However, I can't find the file "MyDemo". There is nothing in 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() {      
    }
}
user1232250
  • 329
  • 3
  • 19
  • http://stackoverflow.com/questions/6146106/where-are-shared-preferences-stored –  Jul 13 '15 at 01:54
  • Excuse me. I have read the above question. My question is I can't find the data/... directory in my Android Device. I think it is because I haven't root my device and no easy way to see the "data" directory. Am I right? – user1232250 Jul 13 '15 at 02:18
  • The answer Skizo gaze you is IMO a good one - yet it's almost exactly the one given http://stackoverflow.com/a/9108524/719662 <- here; also, the referenced question's answers speak of other ways to find that path. Using your device's shell is also a good way to start finding it. As a side note, why do you even need to know the path? –  Jul 13 '15 at 02:25
  • Why I need to know the path? Because I want to have a deep understanding when I learn something. Thanks for your reply. – user1232250 Jul 13 '15 at 02:42
  • just FIY - knowing implementation details or device specifics is hardly a "deep understanding", especially when using a platform that can have literally hundreds of vendor-specific settings - a good software engineer should spend most of the time & effort on working with abstractions, not with low-level implementations. –  Jul 13 '15 at 03:40

1 Answers1

0

You can get the path where you have MyDemo doing this :

File MyDemoFile = getDatabasePath("MyDemo.txt"); //choose your extension
if (MyDemoFile != null){
Log.d("Absolute path : ", MyDemoFile.getAbsolutePath());
}
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148