Drawable.createFromStream(is, srcName);
但是经过调试每次程序都会crash掉,报OOM错误,错误都会打到上面这行代码,搞了老半天,发现BitmapFactory.decodeStream比Drawable.createFromStream好用,后者更占内存。下面是解决后的代码:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.RGB_565;
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream(), null, options);
drawable = new BitmapDrawable(bitmap);
当然options.inSampleSize = 2是设置图片显示的尺寸,这个图片尺寸是随意设置的,可以根据手机屏幕的大小来计算图片需要显示的尺寸。