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 8625279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:44:40+00:00 2026-06-12T07:44:40+00:00

I am 100% sure the problem here is with the actual image. However I

  • 0

I am 100% sure the problem here is with the actual image. However I hope that the solution is some attribute of the image that will help others in the future.

The image:

the photo in question http://soundwave.robotsidekick.com/mlsphotos.jpg

I have tried loading this image in several ways. I have downloaded it and tried loading it in an ImageView:

    final ImageView v = (ImageView) findViewById(R.id.image);
    v.setImageResource(R.drawable.photo);
    v.invalidate();

I have tried loading it from a url:

    final String[] params = new String[] {
            "",
    };

    (new AsyncTask<String, Bitmap, Bitmap>()
            {
        @Override
        protected Bitmap doInBackground(final String... params)
        {
            Bitmap ret = null;
            for (final String url : params)
            {
                try
                {
                    Log.e(TAG, url);
                    ret = BitmapFactory.decodeStream((new URL(url)).openStream());
                    publishProgress(ret);
                }
                catch (final MalformedURLException e)
                {
                    Log.e(TAG, "Malformed URL", e);
                }
                catch (final IOException e)
                {
                    Log.e(TAG, "IO Exception", e);
                }
            }
            return ret;
        }

        @Override
        protected void onProgressUpdate(final Bitmap... values)
        {
            super.onProgressUpdate(values);

            for (final Bitmap result : values)
            {
                if (result != null)
                {
                    final ImageView v = (ImageView) MainActivity.this.findViewById(R.id.image);
                    v.setImageBitmap(result);
                    v.invalidate();
                }
            }
        }
    }).execute(params);

I have also tried loading the image in a WebView like this:

    final WebView webview = (WebView) findViewById(R.id.webview);
    webview.loadData("<html><body><img src=\"" + url + "\"></body></html>", "text/html", "utf-8");
    webview.invalidate();

I have also tried loading the image in Browser (the app) and that does not work.

None of those work, HOWEVER if I load the url into Chrome on Android it works great (not in Browser), if I load the image on my desktop browser (Chrome, Firefox, etc) it loads great. I have checked that the mime type matches the extension and I am just at a loss.

EDIT

There is a work around for images coming from an InputStream where the bitmap processing runs out of data on the stream before the stream completes. The work around is documented in this question and this bug.

However this is a corrupt image whose data ends prematurely. I know that means I am already down a broken track, but I am hoping to have some better error handling than Android passing me back null and I lose. iOS, Chrome (on device and computer) as well as most other places seem to have much better error handling. Is there something I can do on Android to handle corrupt jpgs?

EDIT 2

There has to be a solution here because Chrome on my device handles this situation elegantly. However the closest I can come to fixing this is the following code:

final InputStream is = (new URL(url)).openStream();
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final int size = 1024;
int len = -1;
byte[] buf = new byte[size];
while ((len = is.read(buf, 0, size)) != -1)
{
    bos.write(buf, 0, len);
}
buf = bos.toByteArray();

// buf is now filled with the corrupted bytes of the image

ret = BitmapFactory.decodeByteArray(buf, 0, buf.length);

// ret is null because it was a corrupt jpg

With that code I can check if there are bytes and the image wasn’t decoded. Then at least I can tell I have a corrupt image (or not an image) and can report something slightly more useful to the user (like hey I have an image here with 16K but I sure don’t know what to do with it).

Anyone know how Chrome manages to decode as much of the image as they can before they hit the corruption?

  • 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-06-12T07:44:42+00:00Added an answer on June 12, 2026 at 7:44 am

    Here is the important bit about JPGs from Wikipedia and here’s a question that ultimate led me to the solution.

    I just appended the two closing jpeg end of image bytes to the stream, in order to convince the decoder that the stream is done with image data. This method is flawed because JPGs can have JPGs inside them, meaning appending one set of end of image bytes, doesn’t guarantee that we closed all the images.

    In both solutions below I assume is is an input stream for a JPG image. Also these two constants are defined:

    private static final int JPEG_EOI_1 = 0xFF;
    private static final int JPEG_EOI_2 = 0xD9;
    

    This method we read all the bytes into memory then try to decode the bytes:

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final int size = 1024;
    int len = -1;
    final byte[] buf = new byte[size];
    try
    {
        while ((len = is.read(buf, 0, size)) != -1)
        {
            bos.write(buf, 0, len);
        }
        bos.write(JPEG_EOI_1);
        bos.write(JPEG_EOI_2);
    
        final byte[] bytes = bos.toByteArray();
    
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }
    catch (final IOException ex)
    {
        return null;
    }
    catch (final Exception ex)
    {
        return null;
    }
    

    This method creates a stream wrapper that makes sure the last two bytes are JPG end of image bytes:

    return BitmapFactory.decodeStream(new JpegClosedInputStream(is));
    
    // And here's the stream wrapper
    class JpegClosedInputStream extends InputStream
    {
        private final InputStream inputStream;
        private int bytesPastEnd;
    
        private JpegClosedInputStream(final InputStream iInputStream)
        {
            inputStream = iInputStream;
            bytesPastEnd = 0;
        }
    
        @Override
        public int read() throws IOException
        {
            int buffer = inputStream.read();
            if (buffer == -1)
            {
                if (bytesPastEnd > 0)
                {
                    buffer = JPEG_EOI_2;
                }
                else
                {
                    ++bytesPastEnd;
                    buffer = JPEG_EOI_1;
                }
            }
    
            return buffer;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not 100% sure if the problem is with openTK but it is the
So I have an issue that I'm not 100% sure on how to solve.
Quite a simple problem here and not sure why it's being so complicated. Have
Not sure what the problem is here, but I keep getting the result of
I just wrote some simple sample code to make sure that I had EclEmma
I'm not 100% sure how I should word this question but I'll try my
Hey, not 100% sure what this error means. % for f in * ;
After two years of Android development I'm still not 100% sure about what resources
I'm trying to create a simple search page, but I'm not 100% sure how
I'm sure it's something simple, but I have over 100 errors like: and: I

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.