Beginner Q: What is the best way to access read-only files?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Beginner Q: What is the best way to access read-only files?

Postby ppmoore » Fri Apr 03, 2009 8:07 am

Hello,

After reading the tutorial "Working With Files", I have two more questions.
http://www.anddev.org/working_with_files-t115.html

I've been experimenting with an application that reads a text file containing a dictionary of words. When I sign and publish this app, I want to include the text file in the package. From my code, I access the text file in the /data/app-private directory. On the emulator, I test this by first copying the text file to this directory using eclipse's DDMS perspective, and it works.

When I try to do the same on the G1 phone, I cannot copy the text file to the /data/app-private directory. Browsing the G1's file system using eclipse, I notice that there is a /data directory, but is empty. It seems that this is a read-only file system.

Q1: When preparing the package to install on the phone, how do I specify that the text file is also a part of the package? If the text file is part of the package, how do I specify its location? At the moment, my code specifies the hard-coded location like this:

Code: Select all
String fileName = "file.txt";
String dirName = "/data/app-private";
File filePath = new File( dirName, fileName);
BufferedReader input =  new BufferedReader(new FileReader(aFileName));
try {
  // Loop to count lines in the file
  while( input.readLine() != null ) {
    i++;
  }
}


Q2: I suppose I should avoid hard-coding the file's location. I read earlier in this thread that I should use the activity method openFileInput(). Do I need to specify the file's location in this case?

Code: Select all
FileInputStream stream = openFileInput("file.txt");
InputStreamReader osw = new InputStreamReader(stream);


Many thanks,
Paul
ppmoore
Freshman
Freshman
 
Posts: 9
Joined: Thu Apr 02, 2009 5:16 pm

Top

Postby Alexandro » Fri Apr 03, 2009 8:41 am

The android project has the folder assets. Everything you want to attach to your app should be lying there.
To access data in that folder use AssetManager

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. AssetManager assetManager = getResources().getAssets();
  3.  
  4. InputStream is =assetManager.open("yourfile.txt");
  5.  
  6.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

If the file is directly in assets folder (not in subfolders) you need to provide just the name of the file.
You can also set access mode in open().

About hardcoding.
i just use static final strings.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public static final String MY_FILE= "myfile.txt";
  3.  
  4.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4

and access it by calling MyClass.MY_FILE
That way, if you need to change the filename you'll change it only in 1 place (MY_FILE definition).
Alexandro
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Mar 05, 2009 5:42 pm

Postby ppmoore » Fri Apr 03, 2009 11:31 am

Many thanks Alexandro,

That worked.

I wish there was an easlier way to find this information.
Each time I have a problem, I have to visit a forum to get help.
I wish the documentation and the organization of the documentation was better.

Paul
ppmoore
Freshman
Freshman
 
Posts: 9
Joined: Thu Apr 02, 2009 5:16 pm

Postby 2fast4u » Sun Oct 18, 2009 12:30 pm

Hi,

i can't get that access by AssetManager working and keep getting "FileNotFoundException". I'm using AVD with android 1.6. When debugging and inspecting AssetManager its variables "mAppName" and "mAssetDir" are null. Could that be a reason? Is there some kind of configuration necessary for AssetManager?

I'll post Manifest and Javacode for you guys too to make it easier location the problem.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package de.esg.phoneapp;
  3.  
  4.  
  5.  
  6. import java.io.IOException;
  7.  
  8. import java.io.InputStream;
  9.  
  10. import java.util.HashMap;
  11.  
  12.  
  13.  
  14. import android.app.Activity;
  15.  
  16. import android.content.res.AssetManager;
  17.  
  18. import android.content.res.Resources;
  19.  
  20. import android.os.Bundle;
  21.  
  22. import android.widget.ArrayAdapter;
  23.  
  24. import android.widget.AutoCompleteTextView;
  25.  
  26.  
  27.  
  28. public class ESGphone extends Activity {
  29.  
  30.        
  31.  
  32.         public static final String PHONEBOOK_FILE = "phonebook.json";
  33.  
  34.        
  35.  
  36.         HashMap<String, ContactInfo> entryMap = new HashMap<String, ContactInfo>();
  37.  
  38.        
  39.  
  40.     /** Called when the activity is first created. */
  41.  
  42.     @Override
  43.  
  44.     public void onCreate(Bundle savedInstanceState) {
  45.  
  46.         super.onCreate(savedInstanceState);
  47.  
  48.         setContentView(R.layout.main);
  49.  
  50.        
  51.  
  52.        
  53.  
  54.         try {
  55.  
  56.                
  57.  
  58.                 AssetManager am = Resources.getSystem().getAssets();
  59.  
  60.                         InputStream inStream = am.open(PHONEBOOK_FILE);
  61.  
  62. //                      JSONConnector.parseFile(inStream, entryMap);
  63.  
  64.                
  65.  
  66.                 AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.searchfield);
  67.  
  68.                 ArrayAdapter adapter = new ArrayAdapter(this,
  69.  
  70.                         android.R.layout.simple_dropdown_item_1line, entryMap.keySet().toArray());
  71.  
  72.                 textView.setAdapter(adapter);
  73.  
  74.                  
  75.  
  76.         } catch (IOException e) {
  77.  
  78.                         e.printStackTrace();
  79.  
  80.                 }
  81.  
  82.     }
  83.  
  84. }
  85.  
  86.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.      package="de.esg.phoneapp"
  7.  
  8.      android:versionCode="1"
  9.  
  10.      android:versionName="1.0">
  11.  
  12.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  13.  
  14.         <activity android:name=".ESGphone"
  15.  
  16.                  android:label="@string/app_name">
  17.  
  18.             <intent-filter>
  19.  
  20.                 <action android:name="android.intent.action.MAIN" />
  21.  
  22.                 <category android:name="android.intent.category.LAUNCHER" />
  23.  
  24.             </intent-filter>
  25.  
  26.         </activity>
  27.  
  28.  
  29.  
  30.     </application>
  31.  
  32.     <uses-sdk android:minSdkVersion="4"/>
  33.  
  34.  
  35.  
  36. </manifest>
  37.  
  38.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


I was searching the web for 2 hours now to fix the problem... i hope you guys can help me. And yes.... the file "phonebook.json" is located in the "assets" folder of the project in eclipse ;-)
2fast4u
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Oct 18, 2009 12:19 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests