0

I need to transport several files inside the source code. Horrible, I know, but I don't have any other option. So far I'm only storing images, so I do this:

Encoding the image:

Bitmap bm = BitmapFactory.decodeFile(imagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String encodedimage = Base64.encodeToString(b, Base64.DEFAULT);

Decoding the image:

public static Bitmap decodeBitmap(imageString) {
        return BitmapFactory.decodeStream(new ByteArrayInputStream(Base64.decode(imageString, Base64.DEFAULT)));
    }

I use base64 so that putting a new image in the program is easier because it's just a simple string.

Now I'm trying to expand the functionality to any file format so I seek guidance as to find out if there is less bad way of storing files inside code.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
JohnEye
  • 6,436
  • 4
  • 41
  • 67
  • Got to ask, why? In the name of all that's peace and love, why? – Simon Oct 12 '12 at 15:53
  • I am creating a JAR library that needs to hold some assets. Apparently it is impossible to access them if they are not android resources. It's no big deal, the files are really tiny. – JohnEye Oct 12 '12 at 16:00
  • OK, do you mean it's impossible to access Android resources from the JAR? – Simon Oct 12 '12 at 16:05
  • No, I mean that it's impossible to access assets that are carried in the JAR. They get lost at compile time as they are not part of the Android assets. – JohnEye Oct 15 '12 at 09:22
  • OK, got it. I'm just about to do something similar. If I find anything, I'll come back. For now, nothing "less bad" springs to mind. – Simon Oct 15 '12 at 10:52
  • Thank you. Still, doing it this way makes me think that I just earned a Ph.D. in horribleness. – JohnEye Oct 15 '12 at 13:03

2 Answers2

0

for images you should be using the resources folder. It's built into the system for that. For general final you should use the asset folder and access just like shown on this stackoverflow question: How to get URI from an asset File?

Community
  • 1
  • 1
Budius
  • 39,391
  • 16
  • 102
  • 144
0

If you want to keep some data files, you can keep them in Assets folders and you can use AssetManager to get the files.

Durairaj Packirisamy
  • 4,635
  • 1
  • 21
  • 27