1. Check out our companion app, Forums for Android! Download from Google Play

Trouble Reading Files in Android Code

Discussion in 'Application Development' started by Gravity Games, Dec 6, 2012.

  1. Gravity Games

    Gravity Games Member
    Thread Starter
    15

    Aug 21, 2012
    19
    0
    15
    Hi, I'm having trouble getting an android app to read from a text file. I tried using the code from my Java project, but apparently that's one of the things that are different in Android Dev...

    Code (Text):
    1. public void levelFile(){
    2. try{
    3.     String stage = new String("assets//levels//temple1.txt");
    4. tilescan=new Scanner(new File(stage));
    5. //int b=0;
    6. while(tilescan.hasNextLine()){
    7. tilex=tilescan.nextInt();
    8. tilexstr=Integer.toString(tilex);
    9. tilexarray.add(tilexstr);
    10. tiley=tilescan.nextInt();
    11. tileystr=Integer.toString(tiley);
    12. tileyarray.add(tileystr);
    13.     //b++;
    14. }
    15.     tilescan.close();
    16. }catch(Exception e){System.out.println("error");}
    17. }
     

    Advertisement

  2. jonbonazza

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    Mmmm no, file reading is the same in android and java.

    It looks like your file path is all messed up though. You don't need to escape a '/' character, so your file path is being misinterperated. furthermore, your path is implying that your files are in <app's install dir>/assets/levels/>, which isn't possible in android.

    Are you trying to access your project's asset dir or an actual assets folder on disk?
     
  3. Gravity Games

    Gravity Games Member
    Thread Starter
    15

    Aug 21, 2012
    19
    0
    15
    The assets directory. Also, those backslashes were there because I remember hearing somewhere that you need them. Well, since I don't I'll go remove them.
     
  4. jonbonazza

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    Okay, well the problem is that your assets aren't extracted into the assets directory. In order to access them, you either need to first move them to the SDCard and use the Java file manipuation classes, or use the Android AssetManager class to access them.
     
  5. Gravity Games

    Gravity Games Member
    Thread Starter
    15

    Aug 21, 2012
    19
    0
    15
    Okay, so how do I use the android asset managers? A Google search didn't bring me anything of use.
     
  6. jonbonazza

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    example:
    Code (Text):
    1.  
    2. AssetManager am = context.getAssets();
    3. InputStream is = am.open("relative/path/to/file/in/assets/folder");
    4. //read from input stream
    5.  
     
  7. Gravity Games

    Gravity Games Member
    Thread Starter
    15

    Aug 21, 2012
    19
    0
    15
    No matter whether I declare "is" as a File or an Input Stream, the scanner won't accept it. Also, for some reason "context.getAssets();" gets an error as well.
     
  8. LexiconStudios

    LexiconStudios Well-Known Member
    38

    Jul 14, 2012
    96
    10
    38
    Developer
    Have you already declared your context?
     
  9. Gravity Games

    Gravity Games Member
    Thread Starter
    15

    Aug 21, 2012
    19
    0
    15
    Chances are I didn't. What do you mean by declaring the context of the input stream? (Unless you mean I need the text file in the App Manifest or something...)
     
  10. jonbonazza

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    context is a variable. If you don't have a variable called context already, that code won't work. If you are in an Activity, you can get a pointer to your application's context by doing something like this:

    Code (Text):
    1.  
    2. Context context = getApplicationContext();
    3.  
     
  11. Gravity Games

    Gravity Games Member
    Thread Starter
    15

    Aug 21, 2012
    19
    0
    15
    The problem with this though is that the level code is in the layout, not the activity. Is there a way around this that I'm not seeing? (setting context to static doesn't help...)
     
  12. jonbonazza

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    can you post the code for the class that is having this issue? I want to make sure I understand what you are saying before I suggest anything else that might not apply to your problem. lol
     

Share This Page

Loading...