android:如何从数据库中为AlertDialog设置图标?

android:如何从数据库中为AlertDialog设置图标?,android,Android,因此,我向AlertDialog展示了如下内容: new AlertDialog.Builder(context) .setMessage(message) .setTitle(title) .setCancelable(true) .setIcon(R.drawable.ic_launcher) // set icon // more code 是否可以使用setIcon从db eg联系人照片中获取图标: DatabaseHelper db = new DatabaseH

因此,我向AlertDialog展示了如下内容:

new AlertDialog.Builder(context)
  .setMessage(message)
  .setTitle(title)
  .setCancelable(true)
  .setIcon(R.drawable.ic_launcher) // set icon
  // more code
是否可以使用
setIcon
从db eg联系人照片中获取图标:

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\u url
(像
content://com.android.contacts/data/1
)的设置图标,但它当然希望参数是
int
而不是
string
Uri
。可以实现吗?

例如,您可以使用
位图可绘制
可绘制
的任何其他子类

BitmapDrawable drawable = new BitmapDrawable(bitmap);

AlertDialog.Builder builder = new AlertDialog.Builder(context)
  .setMessage(message)
  .setTitle(title)
  .setCancelable(true)
  .setIcon(drawable);
您还可以在构造函数中直接使用
InputStream
提供
BitmapDrawable
。但您首先必须以某种方式创建一个
位图
实例或
输入流
。具体操作方式取决于图像的存储方式。

以下是:

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)

如何对db中的内容使用
BitmapDrawable
?你能再详细说明一下你的例子吗?谢谢,我认为构造函数
BitmapDrawable
不支持参数。