9

background

suppose i have an inputStream that was originated from the internet of a certain image file.

i wish to get information about the image file and only then to decode it.

it's useful for multiple purposes, such as downsampling and also previewing of information before the image is shown.

the problem

i've tried to mark&reset the inputStream by wrapping the inputStream with a BufferedInputStream , but it didn't work:

inputStream=new BufferedInputStream(inputStream);
inputStream.mark(Integer.MAX_VALUE);
final BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeStream(inputStream,null,options);
//this works fine. i get the options filled just right.

inputStream.reset();
final Bitmap bitmap=BitmapFactory.decodeStream(inputStream,null,options);
//this returns null

for getting the inputStream out of a url, i use:

public static InputStream getInputStreamFromInternet(final String urlString)
  {
  try
    {
    final URL url=new URL(urlString);
    final HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
    final InputStream in=urlConnection.getInputStream();
    return in;
    }
  catch(final Exception e)
    {
    e.printStackTrace();
    }
  return null;
  }

the question

how can i make the code handle the marking an resetting ?

it works perfectly with resources (in fact i didn't even have to create a new BufferedInputStream for this to work) but not with inputStream from the internet...


EDIT:

it seems my code is just fine, sort of...

on some websites (like this one and this one), it fails to decode the image file even after reseting.

if you decode the bitmap (and use inSampleSize) , it can decode it fine (just takes a long time).

now the question is why it happens, and how can i fix it.

3

4 Answers 4

0

I believe the problem is that the call to mark() with the large value is overwritten by a call to mark(1024). As described in the documentation:

Prior to KITKAT, if is.markSupported() returns true, is.mark(1024) would be called. As of KITKAT, this is no longer the case.

This may be resulting in a reset() fail if reads larger than this value are being done.

5
  • didn't test it on kitkat, but i don't get what the docs say now. if is.mark(1024) isn't called, then what is? in any case, the problem i've reported still can't be handled like what i've written. the question now is: how do you do it correctly? Jan 19, 2014 at 0:03
  • This is the documentation of decodeStream. It means it calls is.mark(1024) internally, overwriting your call. How to do it correctly: reimplement decodeStream without the call to is.mark(1024). Else, you need to generate the input stream twice.
    – hdante
    Jan 19, 2014 at 2:05
  • How do I do that? Simple copying from the source code won't be enough, since a lot of it is internal and private and I assume I also need the native code (C/C++) in this case... Jan 19, 2014 at 6:26
  • I'm not sure. I noticed that there's excessive encapsulation in the code, which makes it hard or impossible to reuse it (and that's a common theme both in android library and in java projects in general). If I went through this path, I'd fully copy and paste the whole BitmapFactory.java file, removing the problematic parts. I didn't try it this way, though, I went for generating the input stream twice.
    – hdante
    Jan 19, 2014 at 17:22
  • i think it's better to download the file to the cache and then read from it instead of connecting to the server twice. about the encapsulation, you are correct. i have no idea why they made it so "secure". it's just bitmaps... Jan 19, 2014 at 17:52
0

(Here is a solution for the same problem, but when reading from disk. I didn't realize at first your question was specifically from a network stream.)

The problem with mark & reset in general here is that BitmapFactory.decodeStream() sometimes resets your marks. Thus resetting in order to do the actual read is broken.

But there is a second problem with BufferedInputStream: it can cause the entire image to be buffered in memory along side of where ever you are actually reading it into. Depending on your use case, this can really kill your performance. (Lots of allocation means lots of GC)

There is a really great solution here: https://stackoverflow.com/a/18665678/1366

I modified it slightly for this particular use case to solve the mark & reset problem:

public class MarkableFileInputStream extends FilterInputStream
{
    private static final String TAG = MarkableFileInputStream.class.getSimpleName();

    private FileChannel m_fileChannel;
    private long m_mark = -1;

    public MarkableFileInputStream( FileInputStream fis )
    {
        super( fis );
        m_fileChannel = fis.getChannel();
    }

    @Override
    public boolean markSupported()
    {
        return true;
    }

    @Override
    public synchronized void mark( int readlimit )
    {
        try
        {
            m_mark = m_fileChannel.position();
        }
        catch( IOException ex )
        {
            Log.d( TAG, "Mark failed" );
            m_mark = -1;
        }
    }

    @Override
    public synchronized void reset() throws IOException
    {
        // Reset to beginning if mark has not been called or was reset
        // This is a little bit of custom functionality to solve problems
        // specific to Android's Bitmap decoding, and is slightly non-standard behavior
        if( m_mark == -1 )
        {
            m_fileChannel.position( 0 );
        }
        else
        {
            m_fileChannel.position( m_mark );
            m_mark = -1;
        }
    }
}

This won't allocate any extra memory during reads, and can be reset even if the marks have been cleared.

6
  • I'm not familiar with "FilterInputStream", but as it takes "FileInputStream" instead of a simple InputStream, this means that I need to give it a file that exists on the device, no? If not, did you check that it works? Jun 3, 2014 at 20:59
  • Correct, in my use case I take the network stream and save it to a disk cache. Then my actual image loader always uses a FileInputStream to read from the cache. FilterInputStream is simply a wrapper that allows you to transform an InputStream as it is read. FilterInputStream can take any InputStream, however MarkableFileInputStream is predicated on it being a FileInputStream due to the use of a FileChannel to do the mark & reset functionality.
    – Adam
    Jun 4, 2014 at 21:27
  • I'd recommend using DiskLruCache if you're interested in caching the images. github.com/JakeWharton/DiskLruCache It can really help reduce redundant network traffic and load times one you've got your data the first time.
    – Adam
    Jun 4, 2014 at 21:31
  • The thing is that the question was how to avoid using the disk and do it all using the inputStream, so this isn't much the answer (though it might be correct). In the end though, I decided to use the disk as it's easier and might be better anyway. What is the difference between using what you did and simply using BufferedInputStream? Jun 5, 2014 at 7:42
  • I put it in the answer, but BufferedInputStream can cause, at worst, an entire second copy of the data to be resident in memory while you're reading it. That's how BufferedInputStream provides the functionality it provides, by throwing the data into an intermediate Buffer.
    – Adam
    Jun 5, 2014 at 17:18
-1

whether you can mark / reset a stream depends on the implementation of the stream. those are optional operations and aren't typically supported. your options are to read the stream into a buffer and then read from that stream 2x, or just make the network connection 2x.

the easiest thing is probably to write into a ByteArrayOutputStream,

ByteArrayOutputStream baos = new ByteArrayOutputStream();
int count;
byte[] b = new byte[...];
while ((count = input.read(b) != -1) [
  baos.write(b, 0, count);
}

now either use the result of baos.toByteArray() directly, or create a ByteArrayInputStream and use that repeatedly, calling reset() after consuming it each time.

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

that might sound silly, but there's no magic. you either buffer the data in memory, or you read it 2x from the source. if the stream did support mark / reset, it'd have to do the same thing in it's implementation.

21
  • can't BufferedInputStream just hold the bytes that i will return to soon ? i don't wish to read the entire file or create a new connection since it misses the whole point - i already have the inputStream and its data is also available... Jul 21, 2013 at 16:42
  • it depend on the implementation of the stream that was used to construct the BufferedInputStream. does it support reset()? Jul 21, 2013 at 16:43
  • it should, no? anyway, suppose i use your method, doesn't it mean i download the entire bytes of the file? Jul 21, 2013 at 16:45
  • no, it should not. what you have to understand is that a stream is, well, a stream, as opposed to a buffer. it does not inherently buffer or cache the data after it is read. that's the whole point of a stream. and yes, it means you have to download and store in memory the entire bytes of the file, or you have to read it 2x from the source. those are your options. i'm curious where you think the data would come from. it's either in memory, or read from the source again. there's no magic here. Jul 21, 2013 at 16:46
  • is there an inputStream class that caches the bytes, so that i could read some bytes, and then return back to some position ? i could even tell it how much bytes to support to go back if needed... Jul 21, 2013 at 16:47
-1

Here is a simple method that always works for me :)

 private Bitmap downloadBitmap(String url) {
    // initilize the default HTTP client object
    final DefaultHttpClient client = new DefaultHttpClient();

    //forming a HttoGet request
    final HttpGet getRequest = new HttpGet(url);
    try {

        HttpResponse response = client.execute(getRequest);

        //check 200 OK for success
        final int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode +
                    " while retrieving bitmap from " + url);
            return null;

        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                // getting contents from the stream
                inputStream = entity.getContent();

                // decoding stream data back into image Bitmap that android understands
                image = BitmapFactory.decodeStream(inputStream);


            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (Exception e) {
        // You Could provide a more explicit error message for IOException
        getRequest.abort();
        Log.e("ImageDownloader", "Something went wrong while" +
                " retrieving bitmap from " + url + e.toString());
    }

    return image;
}
1
  • Where is the part of getting the bitmap information (width,height,...) ? Where is the downsampling part? Jun 3, 2014 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.