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 absolute 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.
Related
To test one of my implementations I need a valid and correctly initialised File-Object that points to an image file.
I've tried the following:
placed an image into the androidTest/ressources-Folder named cat.jpeg
tried to create a file handle the following way: new File(InstrumentationRegistry.getContext().getClassLoader().getResource(“cat.jpg”).getPath())
when calling exists() on the initialized file it always returns false.
How do I have to setup the test / initialize the file to make it work?
One option is to put all the test resources in androidTest/assets. By fetching the AssetManager from the test package's context, you can get the assets as a list of filenames (AssetManager.list(String path)). For each asset, open an InputStream with AssetManager.open(String filename) and create a temporary file that you write the asset to.
I have created a java application using "Simple" that allows me to serialize objects in some configuration xml file.
These files must be redistributed with my android application, but here comes the problem:
Where do I place the files? Is it better to place them in the res/xml folder or create a custom path to the assets folder (assets/myfiles)?
In the examples given with Simple if I want to deserialize an object I need an xml file:
File fl = new File ("string to file path");
but if the file is in the resources of my project, how do I get the file?
Searching the internet I found only examples that use a XmlResourceParser but I don't know how to use it with Simple!
If you are going to include files you can later access from your application you'll need to put them in the assets folder. After your application has been installed and running you can get get that xml file by using the AssetManager class:
AssetManager assets = getAssets();
InputStream stream = asset.open("filename");
That will give you an InputStream for you to do whatever you need.
Using Eclipse, Android SDK.
I have a text file full of data that I need pulled in. (For now, it's the easier the way, eventually I'm going to need to scrape dynamic data from a URL, but for now I have the test data I need in this text file).
I've created a class to open this file, but no matter how I try to open it I keep geeting "file not found" exceptions.
I've tried putting my "data.txt" file in various relative paths (within my App):
- "/AppName/"
- "/AppName/src/com/example/appname/data.txt"
I've tried passing different relative paths. I've tried putting the text file in the same path of the .java class file that's trying to open it, and it still can't find it! What am I doing wrong?
What am I doing wrong?
You have two main options of where to store this file within your project directory: assets/ and res/raw/.
If you use assets/, you can call getAssets() on your Activity (or other Context), and on there call open() with the relative path within assets/ to get an InputStream on this file (e.g., assets/data.txt would be accessed via getAssets().open("data.txt")).
If you use res/raw/, you can call getResources() on your Activity (or other Context), and on there call openRawResource(), passing in the R.raw value based upon your filename (e.g., res/raw/data.txt would be accessed via getResources().openRawResource(R.raw.data)).
Use /res/raw directory. I read sound files from raw:
InputStream soundFile1 = context.getResources().openRawResource(soundOneId);
Where context is a base context of activity passed to the class.
http://developer.android.com/reference/android/content/Context.html
I'm developing a test class that have to check if there are all the resources. It's a specification mandatory that i access to the resources by file name (png's).
I know there's the possibility of loading as a resource, but I cannot load resources. I'm developing a test app. How I could access the file file.png in res/drawable named ?
new FileOutputStream("/res/drawable/xxx.png");
It is recommended to read Resources Overview from Android docs,but if your problem is only about accessing resources,read Accessing Resources.So you can do what you want.
Summery,there is some ways to access resources that depends on situations,for example when you know name of resource or you know it's ID.And also it depends that where you want to access resources.
In your case I guess that you want to access a drawable in your code,if it is true,you can do like this:
R.drawable.xxx
For example if name of your .png file is myimage and you have to set it in ImageView with name im do this:
ImageView im = ...;
im.setImageResource(R.drawable.myimage);
Edit:
If you have to get an InputStream form a file in resources,you should consider creating a raw folder inside res directory and put your file in it and then call getResources().openRawResource(resourceName) from your Activity or widget.For example :
InputStream ins = getResources().openRawResource(R.raw.test);
Here your file that is in raw folder is for example test.png.
You can see more details here:
Using files as raw resources in Android
Android: Loading files from the Assets and Raw folders
stackoverflow
stackoverflow
stackoverflow
Finally if you want to create an output file from your resource,get an InputStream from it and write it's content to an OutputStream,you can see a good example here.
I want to create new XML resource IDs at runtime. Just generic XML resources like in the res/xml folder, but from a file on an SD card or anywhere in the filesystem.
I found this solution that says it's possible: How do I generate XML resources at runtime on Android?.
It says to:
Save XML into another external folder (SD card or so)
Create AssetManager over those file(s)
Then create Resources which can be later used as ordinary Android resource stored in APK
I don't understand step 2 since there is no constructor for AssetManager. How do I create an AssetManager for external files?
Thanks in advance,
Barry
By create, I think they mean get the AssetManager:
Context.getAssets()
Then, use the asset manager to open the XML file:
AssetManager.openXmlResourceParser(String fileName)