所以我显示的AlertDialog如下:

new AlertDialog.Builder(context)
  .setMessage(message)
  .setTitle(title)
  .setCancelable(true)
  .setIcon(R.drawable.ic_launcher) // set icon
  // more code

是否可以使用setIcon从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);

我希望能够使用photo_url(以类似于content://com.android.contacts/data/1的db格式保存)与setIcon一起使用,但它当然希望参数是int而不是stringUri。请给我一杯好吗?

最佳答案

这就是如何:

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)

08-29 00:57