Sunday, 27 November 2011

2. How to display on-line pictures in an Android app?

You can provide the pseudocode or the Java code to describe the implementation of this function.

Hint: Have a look at QuizGameActivity, the "getQuestionImageDrawable(int questionNumber)" method.

5 comments:

  1. It decodes stream from URL

    imageUrl = new URL(getQuestionImageUrl(questionNumber));
    InputStream stream = imageUrl.openStream();
    Bitmap bitmap = BitmapFactory.decodeStream(stream);
    image = new BitmapDrawable(getResources(), bitmap);

    ReplyDelete
  2. try {
    // Create a Drawable by decoding a stream from a remote URL
    imageUrl = new URL(getQuestionImageUrl(questionNumber));
    InputStream stream = imageUrl.openStream();
    Bitmap bitmap = BitmapFactory.decodeStream(stream);
    image = new BitmapDrawable(getResources(), bitmap);
    } catch (Exception e) {
    Log.e(DEBUG_TAG, "Decoding Bitmap stream failed");
    image = getResources().getDrawable(R.drawable.noquestion);
    }

    ReplyDelete
  3. imageUrl = new URL(getQuestionImageUrl(questionNumber));
    InputStream stream = imageUrl.openStream();
    Bitmap bitmap = BitmapFactory.decodeStream(stream);
    image = new BitmapDrawable(getResources(), bitmap);

    ReplyDelete
  4. Inistialize URL variable with the required link/url.
    Retreive the input stream of the URL.
    Create a bitmap by decoding the input stream.
    Convert it to a drawable image by using BitmapDrawable.

    try {
    imageUrl = new URL(getQuestionImageUrl(questionNumber));
    InputStream stream = imageUrl.openStream();
    Bitmap bitmap = BitmapFactory.decodeStream(stream);
    image = new BitmapDrawable(getResources(), bitmap);
    } catch (Exception e) {
    Log.e(DEBUG_TAG, "Decoding Bitmap stream failed");
    image = getResources().getDrawable(R.drawable.noquestion);
    }

    ReplyDelete
  5. First generate properrly configured URL object. Open and decode an input stream to URL into a Bitmap object.
    try {
    imageUrl = new URL(getquestionImageUrl(questionNumber) );
    InputStream stream = imageUrl.openStream();
    Bitmap bitmap = BitmapFactory.decodeStream(stream);
    image = new BitmapDrawable(getResourse(), bitmap);
    }
    catch (Exception e) {
    Log.e (DEBUg_TAG, "Decoding Bitmap stream failed");
    image = getResourse().getDrawable(R.drawable.noquestion);
    }

    ReplyDelete