android:如何从数据库中为AlertDialog设置图标?
因此,我向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
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
不支持参数。