我想从网址加载图片,但由于该链接没有扩展名
,因此无效这可以解决吗?
怎么办呢?
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
Bitmap bitImg = getBitmapFromURL("http://t0.gstatic.com/images?q=tbn:ANd9GcRf1EqE2kyW12HSb9gZZ8eTIPqNgVkjFis4GkTTYONIpoQtkIde4zybZ4iAqGlIHQ_pnEX499Oa");
img.setImageBitmap(bitImg);
}
public Bitmap getBitmapFromURL(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
答案 0 :(得分:0)
看看这篇文章,我没有看到他的网址,但我认为Android库会根据数据和标题来计算出来:
BitmapFactory.decodeStream returning null when options are set
另外,不要在网址上添加扩展名,而是更改网址!
不同答案 1 :(得分:0)
load an image from a URL but it doesn't work because the link doesn't have an extension
。废话。 Url.openconnection不会查看扩展名。它不起作用,因为您在LogCat中清晰可见NetworkOnMainThreadException
。从未在LogCat中看到过?请去寻找它。将您的网络代码放在AsyncTask或线程中以防止这种情况发生。