问题描述
public class TestButton extends Activity {
/** Called when the activity is first created. */
ImageButton imgBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgBtn = (ImageButton) findViewById(R.id.image);
//String url = "http://thenextweb.com/apps/files/2010/03/google_logo.jpg";
String url1 = "http://trueslant.com/michaelshermer/files/2010/03/evil-google.jpg";
Drawable drawable = LoadImage(url1);
imgBtn.setImageDrawable(drawable);
}
private Drawable LoadImage(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (Exception e) {
return null;
}
}
}
以上是我用它来从网页加载图像插入的ImageButton的code段。大多数的图像得到显示,但某些URL像上面即URL1之一,Drawable.createFromStream返回null!是什么原因以及如何避免或克服这个问题?
Above is the code snippet which I use to load image from web into ImageButton. Most of the images get displayed , but certain urls like the one above i.e. url1 , Drawable.createFromStream returns null !! What is the reason and how to avoid it or overcome this problem ?
推荐答案
我在同样的问题,今天栽了跟头。发现一个答案,幸运的是:)有一个在SDK中错误,说明或多或少的谷歌组线程。
I've stumbled upon same problem today. And found an answer, luckily :) There is a bug in SDK, described more or less on that google groups thread.
解决方法的工作对我来说是:
Workaround that worked for me is:
private static final int BUFFER_IO_SIZE = 8000;
private Bitmap loadImageFromUrl(final String url) {
try {
// Addresses bug in SDK :
// http://groups.google.com/group/android-developers/browse_thread/thread/4ed17d7e48899b26/
BufferedInputStream bis = new BufferedInputStream(new URL(url).openStream(), BUFFER_IO_SIZE);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos, BUFFER_IO_SIZE);
copy(bis, bos);
bos.flush();
return BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size());
} catch (IOException e) {
// handle it properly
}
}
private void copy(final InputStream bis, final OutputStream baos) throws IOException {
byte[] buf = new byte[256];
int l;
while ((l = bis.read(buf)) >= 0) baos.write(buf, 0, l);
}
和确保不设置缓冲区的大小超过8K,因为操作系统会使用,而不是你设置一个默认大小(日志记录,当然,但我花了一段时间才能发现)。)
And make sure not to set buffers size to more than 8k, because OS will use default size instead of the one you set (logging that of course, but it took me a while to notice that ;) ).
这篇关于CreateFromStream Android中返回null某些网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!