您好我是谷歌眼镜的新手,我想知道我是否可以从网上显示图像作为我的背景图像。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String address = "http://archiveteam.org/images/1/15/Apple-logo.jpg";
Card myCard = new Card(this);
myCard.setText("Hello, World!");
myCard.setFootnote("First Glassware for Glass");
myCard.setImageLayout(Card.ImageLayout.FULL);
myCard.addImage(new URL(address));
View cardView = myCard.getView();
// Display the card we just created
setContentView(cardView);
}
我在几个主题中看到 myCard.addImage(新的URL(地址))是解决方案,但我在该行上收到以下错误。
Card类型中的方法addImage(Drawable)不适用于参数(URL)
任何帮助将不胜感激,提前谢谢!
答案 0 :(得分:0)
您不应该在UI线程上收集图像。那是一个网络操作。
在新线程上创建一个单独的方法来下载图像,然后将其另存为位图。然后将其保存到卡片中;
DownloadImageTask(myCard).execute(address);
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
Card imgCard;
public DownloadImageTask(Card imgCard) {
this.imgCard= imgCard;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
imgCard.setImage(result);
}
}