我想要设置一个图片成为手机的壁纸,但是所有的壁纸功能都只接受位图。我不能使用WallpaperManager 因为我的系统是2.1之前的。
而且我的壁纸是从网上下载的,不在R.drawable里边。
这些代码可能有用
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);
这是一个图像是下载的版本
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);
}
从网上下载的图片你也可以转化为位图,如下:
Drawable drawable = null;
drawable = Drawable.createFromStream(new URL(imgUrl).openStream(), "图片名称");
希望对你有帮助。
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawableinstanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
Bitmap bitmap= Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas= new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;}
用扫描仪扫描出来的图片就成了位图