Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

android - 如何将Drawable转换为位图?(How to convert a Drawable to a Bitmap?)

I would like to set a certain Drawable as the device's wallpaper, but all wallpaper functions accept Bitmap s only.

(我想将某个Drawable设置为设备的墙纸,但是所有墙纸功能仅接受Bitmap 。)

I cannot use WallpaperManager because I'm pre 2.1.

(我的2.1版之前无法使用WallpaperManager 。)

Also, my drawables are downloaded from the web and do not reside in R.drawable .

(另外,我的可绘制对象是从Web下载的,并不位于R.drawable 。)

  ask by Rob translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This piece of code helps.

(这段代码有帮助。)

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                                           R.drawable.icon_resource);

Here a version where the image gets downloaded.

(这里是下载图像的版本。)

String name = c.getString(str_url);
URL url_value = new URL(name);
ImageView profile = (ImageView)v.findViewById(R.id.vdo_icon);
if (profile != null) {
    Bitmap mIcon1 =
        BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
    profile.setImageBitmap(mIcon1);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...