Which is faster way to access files? - android

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.

Related

How to copy assets to cache in Android?

I need to copy all files from assets folder to android cache to load this data faster. The main reason is loading it once during the start of the app. In the whole app lifecycle, I could access needed files from the cache which would be probably faster. I was searching on the Internet but found nothing. How to do that?
One Android "cache directory" is obtained by calling getCacheDir() on some Context, such as an IntentService or JobIntentService. Copying a bunch of content out of assets into files on the filesystem will take some time, and so a Service with a background thread may be appropriate. Doing the actual copying is a matter of:
Getting an InputStream on the desired asset from AssetManager
Getting a FileOutputStream on where you want to write the content to, such as a file inside of getCacheDir()
Using standard Java I/O to copy the bytes
However, please understand that "cache" is not some magic pixie dust that you spread over an app to make it faster. For example, getCacheDir() is not faster than getFilesDir(), or getExternalCacheDir(), or getExtenalFilesDir(), because they all point to the same hardware (on most devices). Files on the filesystem may be faster to access than are assets, since assets are stored in the APK and require a bit of work to read them out of the APK. So, this may help a bit.
However, since you have not used method tracing, or Log statements, or anything to determine where your time is being spent, it is entirely possible that you will go through this work and get no net improvement. For example, my main book is published as an APK, among other formats. That book has 200+ chapters, all stored as HTML in assets. I do not find that loading that HTML is especially slow. It is possible that using files rather than assets will help you more than it might help me, due to the nature of what you are doing in those pages.
But it is also possible that your performance issues come from:
JavaScript doing too much work
Forgetting that you have a bunch of things that you are downloading from the network, because the URLs to them are buried somewhere (e.g., images referenced in CSS files), and it takes a while for those images to download
Something else that you are doing in your app, while simultaneously you are trying to load this Web content, and so you are overloading the CPU of the device
And so on

Android, SD-card and resource path

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.

Where to safely put large data in android

I have to store a lot of images that have to be downloaded from the web server. The size of the images might be 80Mb. So I want a guidance where to store them, whether in internal or external storage. Both create some problems for me. Internal storage is as every one knows is very limited but the problem with external storage is that images can be accessed by user. I don't wanna my application images to be exposed to user and changed or deleted. So is there any alternative or is there any technique to safely put data into external storage?
Unfortunately no, external storage has FAT file system, which does not support access restriction. And you simply must not store such large chunks of data in internal memory (or otherwise users will not like you, to put it mildly).
So the only way to go, is to use external storage. If you need some protection, then you may either encrypt/decrypt data. Or just obfuscate data, like changing file extensions, or adding 10 bytes at the beginning of each file. Obfuscation is more efficient resource-wise, but much less protected. Though encryption key can still be extracted from your application, so both of this approaches have their flows.
I would advice to store them in the external storage. If you don't want the user to be able to read it, protect it with an encryption. I think it's a bad idea to impose large data to the user. If the user wants to remove it, you shouldn't want to prevent it. Perhaps consider the possibility of re-download the pictures from the web if it has been deleted.
Use encryption for file content:
i found nice and lightweight sample code on http://www.androidsnippets.com/encryptdecrypt-strings
I recommend saving to external. Preventing the user from deleting his data is not recommended. Also user can format the sdcard to delete it. so you cannot stop the user. You can hide it from him. Just prefix a dot to the folder name to make it hidden.
If you are using Android 2.3, OBB is your choice.
http://developer.android.com/reference/android/os/storage/StorageManager.html
OBBs contain a filesystem that maybe be encrypted on disk and mounted on-demand from an application. OBBs are a good way of providing large amounts of binary assets without packaging them into APKs as they may be multiple gigabytes in size. However, due to their size, they're most likely stored in a shared storage pool accessible from all programs. The system does not guarantee the security of the OBB file itself: if any program modifies the OBB, there is no guarantee that a read from that OBB will produce the expected output.
Related
What is OBB(Opaque Binary Blob) in Android develop site?

How to handle large amount of data in android

My app has several image and media files, which are around 1MB each or so. So if i follow the normal way, the app size is crossing over 40MB, which is huge. Is there anyway to avoid this?
I have heard of external storage, but i really don't get any clue of how to work on them!
Do i need to ask all those who instal this to save the images and media files in the external disk and then the app uses those? This makes my files public..isn't it?
I actually don't own a android device. So is it like, whenever people install an app from the market, does it ask if it has to install in the phone memory or the external memory?
I really need your help.
If there is a way, i'd be thankful if you can provide me the step by step details of how this can be done!
Thanks a lot..
Regards
Nithin
There is, from the little I know of this, a slight security risk from putting files onto the SD card. I don't think I personally would worry too much about that since most people that would want access to the files in your apk (Which does not include your source code) could get it regardless without too much trouble.
As of Android 2.2 the user has the option to move an app to their SD card, but only if the developer explicitly tells the app to allow it. I'm fairly sure this only applies to 2.2+ devices though, so being that you are likely going for a larger audience than that it isn't an end-all solution. I am only really pointing this out in case you do end up putting one large file on the market. If so, be sure to allow the transfer to SD card, your app will stay on devices much longer.
Downloading the files online from within the app and saving them out to the SD card would be a good solution, though I am not sure how end users feel about downloading a small app then having to download a very large package before using it. In the end they will have to download it either way, so it is up to you whether you want to ask them to do it up front in the market or afterwards via the app. If you do want to try to download all the content then maybe the code example in this link will help you figure it out :
http://androidsnips.blogspot.com/2010/08/download-from-internet-and-save-to-sd.html
You might consider streaming the files or downloading them inside the application to the sdcard. Speaking from experience my users have had problems downloading apps as big as 30MB. Some phones also have a severely limited internal memory, which is where the applications are downloaded to.

How can I package binary files with my APK, and copy them to sdcard

I have 2 binary files that i would like to package with my apk. (/res/raw)
i need to copy these 2 files to /sdcard when the application is run
how can i do this?
We have the same issue ... the direction we are exploring is to have two separate installs - the first one is the app and the second one is the data-app. When the data-app installs it copies the binary files to the SD card. When we uninstall the data-app it frees up the internal storage.
We don't have this one completely licked yet, and would love to hear other input and maybe find someone to help us by writing a couple of skeletal sample applications for us.
There are so many people who are in this boat (based on my googling) that if this approach doesn't work I suggest we (or someone) set up a generic file delivery web server and generic file delivery Android service and make it available to developers for a very low cost.
You need to use the AssetManager.
That will give you can InputStream that you can copy to a FileOutputStream.
It all depends on what your goal is by doing this.
Are you trying to be nice to the user and conserve disk space on the device by moving files to the sdcard? Or do you merely want to ensure that these files are on the sd card?
If you just want to put the files on the sdcard then you should use the AssetManager as CaseyB mentioned
If you are trying to conserve phone memory then consider distributing the apk file without the 2 raw files, and then on first run download the files from a server that you have set up. This may cause a bit of a problem due to the time needed to download the files, but some users on devices with limited memory available on the device itself will be appreciative of it.

Categories

Resources