• 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

Gravity Games

Newbie
Aug 21, 2012
19
0
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?
 
Upvote 0
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...)
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones