Loading JPEG from RAW resources - android

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

Related

How to provide a text file within an Android app?

Where do I put a text file that I want deployed with my app such that I can provide within my app a path to that text file. I have an included JNI library which will take the text file at that path and perform actions on it. So in other words, I don't think I can just put it in my assets folder without reading the file and resaving it to SD card or something (as I don't think you can reference an assets file directly by path right?). Is there a way around this?
You can place files in res/raw/ and read them as raw resource files/streams.
See https://stackoverflow.com/a/15934525/5734895 for reading raw resources.

How can i insert a .bin created on desktop file into apk

I have code for creating an internal file, there is random algorithem that create the data stored in it and i want any app to have the same file with the same binary data in it.
so i need to make the file on my desktop and add it to internal files some how.
my question is what do you think is the best way to do it.
i thought to locate it in my project, read it, and write it to internal files.
the problem is, i dont know where to locate my file in android studio so that it will be included in the external files and then where to read it from.
thanks. =]
hope i made myself clear.
Put it in src/main/assets/.
You can then access your file with AssetManager and do whatever you want with it.
From the Android Developers website:
main/assets/
This is empty. You can use it to store raw asset files. Files that you
save here are compiled into an .apk file as-is, and the original
filename is preserved. You can navigate this directory in the same way
as a typical file system using URIs and read files as a stream of
bytes using the AssetManager. For example, this is a good location for
textures and game data.
You need to move that into the assets folder. From there you can refer to the file.

Manage Large APK Expansion File

I'm using an expansion file (~500mb) that contains videos and more than 20.000 image files.
Which is the best way to manage this file?
For now, i'm loading the ZipFileResource object in my Application class when i check if this file is already downloaded.
One of the uses for this expansion file, is to provide images for a gigapixel view, but it takes a lot of time to load one image, and it should load about 30 images in 200ms.
Before i move this image files to an expansion file, there were in assets folder and worked well.
I'm planning unzip the expansion file at the external storage and remove the obb file, but i don't know if it's a good choice.
Can you tell me some advices?
Thanks in advance.
As far as I understood you, you'll download your mediafiles after you have installed the App. For this I would make it like that, download it and save it to the SD-Card (I would do this in an AsyncTask). Now you save every path of your picture or video into your DB. If you want to get the picture read it out the db and fill your ImageView or whatever. This way is really fast.
I hope I could help you.
safari
I tried to use Zip files as extension files once and I have found that they work very slowly.
OBB files works fast, just as with your regular file system.
I'm planning unzip the expansion file at the external storage and remove the obb file, but i don't know if it's a good choice.
It's bad choise. Google doesn't recommend:
To ensure proper behavior, you must not delete, move, or rename the expansion files.

How to provide some resource files for an android application?

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.

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.

Categories

Resources