0

Hello I am new with Google Glass and am wondering if I can display an image from the web as my background image.

@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);
}

I saw in a few threads that myCard.addImage(new URL(address)) was the solution, but I am getting the following error on that line.

The method addImage(Drawable) in the type Card is not applicable for the arguments (URL)

Any help would be appreciated, thanks in advance!

user3562751
  • 263
  • 1
  • 5
  • 15
  • You can send images from the web to your application using [Mirror APIs](https://developers.google.com/glass/develop/mirror/index). – G3M Jul 21 '14 at 18:18

1 Answers1

0

You shouldn't be gathering images on the UI thread. That is a network operation.

Create a separate Method running on a new thread to download the image, then save it as a bitmap. Then save that to the card;

 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);
}
}
johng
  • 825
  • 1
  • 9
  • 15