I have a read only SQLite database that's about 40mb uncompressed, and I'd like to add this to a resource path on the SD card. (In the same way I might have /res/drawable or /assets etc.)
Can I do this as part of the project's file structure on all versions of Android (2.2 or later say) and on all devices?
(I don't want to do this within code, that's not an option. And I've set the manifest to declare prefersExternal.)
Installing a program with such a large asset size will be an issue on a large number of mobiles with small internal storage and no SD card, however, tablets do tend to have large internal storage. I believe moving the file from the RAW folder will be your only way (/res/raw)
Have a look at this approach to spilting a database file into multiple sub files and then merging them on 1st run, but this does require code to access and create the usable database file.
Also consider compressing the file beforehand as well and then remember to decompress later as well. High compression rates can be obtained with database files. This will have the extra bonus of meaning your application's installer won't be quite as big.
Related
Friends
I'm curious about which one is the faster way.
If i access/read the files from Assets folder of the project?!!
OR
If i access/read the files from the SDcard??!!!
Based on the performance of the application which one of is the better way?
Please can anyone explain me with reason?
Any help will be appreciated.
Thank you.
Based on the performance of the application which one of is the best way
When you are adding files to the assets folder your whole application's size increases, hence you will require more of your device's RAM to execute the application. While the app will react faster it also requires more memory to run.
But when you are storing that file on SD card then you will require less RAM to load the your application. Fetching files from SD card will reduce your performance more than the first option but the difference is little.
I want to add some more here, you need to consider your file size too in case of access performance. Referring from This, Files over 1Mb placed in the Assets folder won't be readable from your application, will throw exception. Because files got compressed during the build process, and thus a phone requires resources to uncompress them when they are on the handset.
Generally, when your file size is small, definitely reading from asset folder is faster than reading from sd card. But in my opinion, if you consider file size and exception security, then it is safer to read it from sd card.It increases overall system performance rather than some minor time difference which isn't visible in your eye infact.
I need 6 large images in my app. And I don't need the quality and alpha channel provided by PNG. Is there a way to embed JPEG files into the raw resource and unfold them into SD card at first launch of the program? It will save me 3-4 MB in my APK. Thanks!
It's very easy. Put your file in res/raw filder and use:
InputStream is = getResources().openRawResource(R.raw.image);
BitmapFactory.decodeStream(is);
You can simply open the stream and do whatever you want with the data.
I think I don't understand your question: It seems that you want to hive off image files at first run...
Anyway, I suggest to put the files in the assets folder and not in the resource. You can access the assets as a file system and copy them to any (permitted) location.
Have a look here:
Difference between /res and /assets directories
and here (look at assets):
http://developer.android.com/tools/projects/index.html
EDIT:
My answer suggests to use assets instead of the resources but, you can't modify your apk at runtime, look here:
how we can remove a file from the assets folder at runtime in android?
Nothing prevents you to put JPEG images in the resource folders, or in the assets folder if you don't need the R.drawable.MY_IMAGE thingy.
However, the images would still be included in your APK, and cannot be removed from your application package even after you copied them to the SD card.
The only way is to download the images separately from a web server on your application first launch.
The Google Play Store also provides some facility if your application needs big files, but that seems a bit of an overkill
When I complete the Android app I am developing I will want to distribute it so that about 300 image files are stored on the sdcard. Is there any way to do this within the usual automatic installation system? I don't want to hog the internal memory by including the files in res/raw.
I believe the app can now be built with an instruction to install on the sdcard so I suppose I could do that and include the files in res/raw but is there any limit on the number of files in res/raw?
If you didn't want to include the images you could utilize the APK Expansion Files mechanism:
http://developer.android.com/google/play/expansion-files.html
It will even save it to the sdcard.
Otherwise, you will either have to include the images or download them programmatically to the sdcard. As for the limits of the res/raw/assets folder I couldn't find any hard figures. The upper limit though would be the max size of the apk. You could always write a little script that would place 5k/10k/20k 1byte files and see for yourself on that upper bound. If the actual amount of files became an issue you could always zip them in the assets/raw and unzip them to external storage.
I have personally shipped an application for a client that had upwards of 500 images in the assets folder and it worked very well.
I'm writing an android application, which user can download some image files from server. There image files will be stored in android mobile.
Now I want to put some of image files inside the apk file, that user can start the application quickly after installing. I found I can put them into assets directory, but the directory is read only. When user download other image files, I need to store them into another directory.
So there will be two directories to store the image files, but they are the same type.
Is there any good solution for this case?
Check out http://developer.android.com/guide/topics/data/data-storage.html#filesInternal for a listing of different places you can put data on Android.
You should put downloaded image files into one of three places, depending on your needs.
If the images are meant to be viewable by the user (e.g. downloaded photos), put them on the external storage. If they are meant to be user-interface elements or other crucial (but not user-facing) images, put them on internal storage. If they are meant to be cached for quick access but downloaded if necessary (e.g. temporary images like those found on a website), put them in the internal cache directory (Context.getCacheDir()).
If you don't have a lot of assets, you can copy them to the target location when your program first runs (e.g. check for the existence of a certain file, and create that file when you are done setting up). Then you only have to check one place (unless it's the cache dir, in which case you can't guarantee that the files will stick around forever).
If you have a lot of asset files, I would use a two-stage lookup: consult your downloaded image directory first (so you can override builtin assets, for example), then consult your assets directory. This is also flexible enough to allow you to make use of multiple storage locations should you find the need.
I am working on a library app and have books stored as .sqlite files. Each book's sqlite database file is about 10MB in size. I first tried to put it in assets folder and then copying it to database/ folder but since the file is >1Mb this gives me an IOException. Then I tried to access it from raw folder but its still giving me IOException. So, what is the correct way to access such a file. Also, in future the app might need to download such files from server, so in that case where should I store such database files?
Thanks!!
Before Android 3.0 you are not allowed to open files larger than 1mb.
From the link: [P]rior to Android 2.3, any compressed asset file with an uncompressed size of over 1 MB cannot be read from the APK.
Here are some solutions:
Perhaps you want to look into shrinking the size of the databases, and that might be by making your own, or by removing some of the entries that you do not need/want.
Another solution would be to offload the databases to a server that you have access to and require the application to access the web to get the data that is necessary from this server. I can imagine a nice RESTful API to do this.
There is a workaround to this issue. The limitation of 1MB is only for SQLite files and not for other file types. Rename your SQLite file as something like "db.mp3" and then when your app starts, you can copy this file to your SD Card and use it as a normal SQLite file from SD card.
I have implemented this solution and it works perfectly fine on all Android versions.
Have the app download the database from a web server to the sdcard - this saves the waste of storing both a compressed version in the .apk and an uncompressed version outside. And it's not really any more insecure as an .apk is just a zip file anyone who really wants to can read.