Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7699853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:34:35+00:00 2026-05-31T22:34:35+00:00

I’m working on some code which fills a GridView full of images that have

  • 0

I’m working on some code which fills a GridView full of images that have been pulled from YouTube. Now its working apart from when it can’t find an image, I’m not sure why it can’t find the image as the image link works in a browser but this actually works to my advantage as it weeds out videos that have been removed due to copyright/account closure etc.

My code to get the image is as follows

Call

        final Object item = allMatches.get(position);
        if(item != null) {
        Bitmap bitmap = loadBitmap("http://i2.ytimg.com/vi/"+ allMatches.get(position).toString() + "/default.jpg"); 
        imageView.setImageBitmap(bitmap);
        }
        else { imageView.setImageDrawable(getResources().getDrawable(R.drawable.default1)); }

loadBitmap()

    public static Bitmap loadBitmap(String url) 
  {
      Bitmap bitmap = null;
      InputStream in = null;
      BufferedOutputStream out = null;

      try {
          in = new BufferedInputStream(new URL(url).openStream(),  4 * 1024);

          final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
          out = new BufferedOutputStream(dataStream,  4 * 1024);

          int byte_;
          while ((byte_ = in.read()) != -1)
              out.write(byte_);
          out.flush();

          final byte[] data = dataStream.toByteArray();
          BitmapFactory.Options options = new BitmapFactory.Options();
          //options.inSampleSize = 1;

          bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
      } catch (IOException e) {
          Log.e("","Could not load Bitmap from: " + url);
      } finally {
          try{
              in.close();
              out.close();
          }catch( IOException e )
          {
              System.out.println(e);
          }
      }
      return bitmap;

So this will sometimes crash with the following Stacktrace

03-23 01:47:10.087: E/(9757): Could not load Bitmap from: http://i2.ytimg.com/vi/8s3lnGuyC9M/default.jpg
03-23 01:47:10.247: W/dalvikvm(9757): threadid=1: thread exiting with uncaught exception (group=0x40018560)
03-23 01:47:10.267: E/AndroidRuntime(9757): FATAL EXCEPTION: main
03-23 01:47:10.267: E/AndroidRuntime(9757): java.lang.NullPointerException
03-23 01:47:10.267: E/AndroidRuntime(9757):     at com.myapp.Sample.loadBitmap(Sample.java:159)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at com.myapp..Sample$ImageAdapter.getView(Sample.java:207)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.widget.AbsListView.obtainView(AbsListView.java:1467)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.widget.GridView.onMeasure(GridView.java:935)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.view.View.measure(View.java:8330)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.view.View.measure(View.java:8330)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.view.View.measure(View.java:8330)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.view.View.measure(View.java:8330)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.view.View.measure(View.java:8330)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.view.ViewRoot.performTraversals(ViewRoot.java:843)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.os.Looper.loop(Looper.java:130)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at android.app.ActivityThread.main(ActivityThread.java:3835)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at java.lang.reflect.Method.invokeNative(Native Method)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at java.lang.reflect.Method.invoke(Method.java:507)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-23 01:47:10.267: E/AndroidRuntime(9757):     at dalvik.system.NativeStart.main(Native Method)

What I need to do is just be able to deal with it not being able to retrieve the image. My Java skills are limited but I want something like the below (but actually working)

Bitmap bitmap = loadBitmap("http://i2.ytimg.com/vi/"+ allMatches.get(position).toString() + "/default.jpg"); //  

if (bitmap == null) {
   imageView.setImageBitmap(defaultBitmap); 
} else {
imageView.setImageBitmap(bitmap); }

or

//Doesn't work anyway due to 'Exception IOException is not compatible with throws clause in Adapter.getView'
try { 
Bitmap bitmap = loadBitmap("http://i2.ytimg.com/vi/"+ allMatches.get(position).toString() + "/default.jpg"); 
imageView.setImageBitmap(bitmap);
}catch( IOException ) {
imageView.setImageBitmap(defaultBitmap);
}

My understanding of try catch blocks is that if you ‘catch the error’ the code should continue. In this case shouldn’t it just be printing the error to the log and carrying on?

How can I do this for when ‘loadBitmap’ fails at retrieving the image?

EDIT: I have updated the code as per some user comments – it still provides the exact same error, it just removes some doubt around the point of failure. Also, to clarify

Line 159: in.close();
Line 207: Bitmap bitmap = loadBitmap("http://i2.ytimg.com/vi/"+ allMatches.get(position).toString() + "/default.jpg"); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:34:36+00:00Added an answer on May 31, 2026 at 10:34 pm

    First:

    try
    {
        Bitmap bitmap = loadBitmap("http://i2.ytimg.com/vi/"+ allMatches.get(position).toString() + "/default.jpg"); 
        imageView.setImageBitmap(bitmap);
    }
    catch(IOException e)
    {
        System.err.println("Couldn't load image: "+e.getMessage());
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.default1)); 
    }
    

    Second:

    public static Bitmap loadBitmap(String url) throws IOException
    {
        final InputStream in = new BufferedInputStream(new URL(url).openStream(),  4 * 1024);
    
        try 
        {
            final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            final BufferedOutputStream out = new BufferedOutputStream(dataStream,  4 * 1024);
    
            try
            {
                int byte_;
                while ((byte_ = in.read()) != -1)
                    out.write(byte_);
                out.flush();
    
                final byte[] data = dataStream.toByteArray();
                BitmapFactory.Options options = new BitmapFactory.Options();
                //options.inSampleSize = 1;
    
                return BitmapFactory.decodeByteArray(data, 0, data.length,options);
            }
            finally
            {
                out.close();
            }
        } 
        finally 
        {
            in.close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.