Problem using AssetManager openFD()
I have a DB helper class which extends SQLiteOpenHelper and in onCreate, I want to read various text files which each contain a single SQL CREATE command. The text files are in my project's asset folder and I'm trying to use a FileReader as follows...
@Override
public void onCreate(SQLiteDatabase db) {
AssetManager am = context.getAssets();
FileReader tableListFileReader = new FileReader(am.openFd(context.getString(R.string.db_schema_filelist_filename)).getFileDescriptor());
I get an exception as follows...
java.io.FileNotFoundException: /npvr_db_schema_create_tables.txt
This obviously seems to be an a开发者_Python百科bsolute path from root ('/') whereas other AssetManager 'open' methods are documented as accessing files in the 'assets' folder. Unfortunately those methods return an InputStream which can't be used to directly instantiate a FileReader.
This doesn't make sense to me - surely AssetManager should be just what it says it is and simply give access to my assets folder. Am I missing something here? How should I approach this in a better way?
Here is an API demo of reading from an asset. The file name semantics are the same as for opening an FD:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/content/ReadAsset.html
The asset is added by putting in an "assets" directory at the same level as your "res" directory. (Look at the ApiDemos full source code to see this.)
It is... unusual... to get the file name of an asset to load from a string resource. You probably don't want to be doing that.
精彩评论