Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

android - how to get bitmap information and then decode bitmap from internet-inputStream?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...