android: how to set icon from database for AlertDialog?

android

Solution

This is how:

Drawable drawable = null;

try {

    DatabaseHelper db = new DatabaseHelper(context);
    Cursor csr = db.getSpecialContact(number);
    csr.moveToFirst();
    String photo = csr
        .getString(csr.getColumnIndexOrThrow("photo_url"));
    Uri photo_url = Uri.parse(photo);

    Bitmap tempBitmap;
    tempBitmap = BitmapFactory.decodeStream(context
        .getContentResolver().openInputStream(photo_url));

    // Convert bitmap to drawable
    drawable = new BitmapDrawable(context.getResources(), tempBitmap);

} catch (FileNotFoundException e) {
    Bitmap bm = BitmapFactory.decodeResource(context.getResources(),
        R.drawable.ic_launcher);
    drawable = new BitmapDrawable(context.getResources(), bm);
}

new AlertDialog.Builder(context)
    .setMessage(message)
    .setTitle(title)
    .setCancelable(true)
    .setIcon(drawable)

Problem

So I am showing AlertDialog something like this: ``` new AlertDialog.Builder(context) .setMessage(message) .setTitle(title) .setCancelable(true) .setIcon(R.drawable.ic_launcher) // set icon // more code ``` Is it possible to use `setIcon` to have icon from db eg contact photo: ``` DatabaseHelper db = new DatabaseHelper(context); Cursor csr = db.getSpecialContact(number); csr.moveToFirst(); String photo = csr.getString(csr.getColumnIndexOrThrow("photo_url")); Uri photo_url = Uri.parse(photo); ``` I want to be able to use `photo_url` (saved in db like `content://com.android.contacts/data/1`) with `setIcon` but of course it expects parameter to be `int` not `string` or `Uri`. Can that be acheived please ?

Original source