所以我显示的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
而不是string
或Uri
。请给我一杯好吗? 最佳答案
这就是如何:
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)