2

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!

rommex
  • 43
  • 1
  • 5

3 Answers3

2

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.

Zielony
  • 16,239
  • 6
  • 34
  • 39
0

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

nicopico
  • 3,606
  • 1
  • 28
  • 30
0

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?

Community
  • 1
  • 1
Seraphim's
  • 12,559
  • 20
  • 88
  • 129
  • Thanks for you reply. The was no question about modifying APK, I just want to shrink the APK -- and JPEGS will help me over PNGs. I guess assets is the right place for this. I only need some code snippets or links where I can read about copying files from assets to SD card. Thanks! – rommex Mar 19 '13 at 21:02
  • here: http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard :) – Seraphim's Mar 19 '13 at 21:09
  • and remember, if you draw these images with corel draw or illustrator, you can add the svg to assets or resources and generate raster images at run time. Obviously this not apply to "photographic" images. – Seraphim's Mar 19 '13 at 21:16
  • but svg files will require some external library to handle them, right? – rommex Mar 19 '13 at 21:28
  • yes, but trust me, it's very simple: http://code.google.com/p/svg-android/ all graphic for my UI comes from svg format. A lot of UI and my app is only 1.5 Mb – Seraphim's Mar 19 '13 at 21:32
  • I'll try it out, because I need vector for some other functions in my app. Many thanks! – rommex Mar 20 '13 at 00:15