• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Trouble Reading Files in Android Code

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:
public void levelFile(){
try{
	String stage = new String("assets//levels//temple1.txt");
tilescan=new Scanner(new File(stage));
//int b=0;
while(tilescan.hasNextLine()){
tilex=tilescan.nextInt();
tilexstr=Integer.toString(tilex);
tilexarray.add(tilexstr);
tiley=tilescan.nextInt();
tileystr=Integer.toString(tiley);
tileyarray.add(tileystr);
	//b++;
}
	tilescan.close();
}catch(Exception e){System.out.println("error");}
}
 
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?
 
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.
 
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.
 
example:
Code:
AssetManager am = context.getAssets();
InputStream is = am.open("relative/path/to/file/in/assets/folder");
//read from input stream
 
example:
Code:
AssetManager am = context.getAssets();
InputStream is = am.open("relative/path/to/file/in/assets/folder");
//read from input stream

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.
 
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...)
 
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:
Context context = getApplicationContext();
 
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:
Context context = getApplicationContext();

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...)
 
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
 
Back
Top Bottom