1

I'm wondering what the best way to store a local phone contact's icon is. I'm writing a manager that will allow the user to select contacts for display in a list which now needs contact icon display.

Would storing the icon in a different location on select work or should I try the route of storing the location of the icon and linking to it that way? Can anyone who has went through this already point me in the right direction? I'm using an Sqlite database to store the contacts already.

Any code/links would be very helpful.

wajiw
  • 12,239
  • 17
  • 54
  • 73

2 Answers2

1

Since you are already storing the contacts in an Sqlite db, I'd just add another field to that db that will hold an encoded image.

The way I have gone to solve a similar issue is: I used Base64 for encoding an image into a String and then I store that String wherever I want...

I added one function to the Base64 class to directly encode a Bitmap object for me and return a String, here's the code:

public static String encodeBitmap(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] buf = stream.toByteArray();
    return encodeBytes(buf);
}

where encodeBytes(buffer) is already an implemented function of the Base64 class.

This would be a better way of doing it than storing the path to the image, because a user can easily change the path and then your application wouldn't find the picture anymore.

Hope that helps.

Gligor
  • 2,862
  • 2
  • 33
  • 40
1

First, I'd recommend looking at the Android source and seeing how they do it.

That aside, if the images are smallish, I'd highly recommend creating a proper ContentProvider and storing the images as binary blobs attached to the row using Cursor.getBlob(). See http://developer.android.com/intl/de/guide/topics/providers/content-providers.html#querying for more details.

For larger images, take a look at How to store large blobs in an android content provider?

Community
  • 1
  • 1
Steve Pomeroy
  • 10,071
  • 6
  • 34
  • 37