0

I want to get bitmap from a inputstream, then re-size it. But I am getting below error.

If I return without re-sizing, it works fine.

Can anybody help please?

LOGCAT:

01-07 01:38:33.412: D/skia(1307): --- SkImageDecoder::Factory returned null

CODE:

private Bitmap getBitmap(String url)
{
    try {
        Bitmap bitmap=null;
        URL imageUrl = new URL(url);

        HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
        conn.setConnectTimeout(10000);
        conn.setReadTimeout(10000);
        conn.setDoInput(true);
        conn.setInstanceFollowRedirects(true);
        conn.connect();
        InputStream is=conn.getInputStream();

        //return BitmapFactory.decodeStream(is); // THIS WORKS FINE
        bitmap = decodeFile(is);
        is.close();
        return bitmap; 
    } catch (Exception ex){
       return null;
    }
}

private Bitmap decodeFile(InputStream istream){
    BufferedInputStream is = new BufferedInputStream(istream);
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is,null,o);

        final int REQUIRED_SIZE=60;
        int height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(height_tmp/2<REQUIRED_SIZE)
                break;
            height_tmp/=2;
            scale*=2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        Log.d("insample", "aa: "+scale);
        return BitmapFactory.decodeStream(is, null, o2);
    } catch (Exception e) {
        Log.d("aaa","aa: "+e);
    } finally{
        try {
            is.close();
        } catch( IOException ignored ) {}
    }
    return null;
}
abdfahim
  • 2,523
  • 10
  • 35
  • 71
  • check [this](http://stackoverflow.com/a/13062314/2388614) answer. – mrsus Jan 07 '14 at 06:50
  • actually I tried that before and got IOException (if I reset InputStream) or "Mark Invalidated" error (if I reset bufferinputstream) ... so I checked markSupported(), and found it as false. But not sure how to mark it ..... by 32K or 8K or what? – abdfahim Jan 07 '14 at 07:07
  • see the comment made by the guy on [this](http://stackoverflow.com/questions/10864729/skimagedecoderfactory-returned-null) question. – mrsus Jan 07 '14 at 07:42
  • I don't think that is my issue, as if I directly return BitmapFactory.decodeStream(is) from getBitmap method, it works .... my problem arises if I try to resize it in decodeFile() method. – abdfahim Jan 07 '14 at 08:55
  • if you move your pointer on `decodeStream()`. It says,`It returns decoded bitmap, or null if the image data could not be decoded, or, if opts is non-null, if opts requested only the size be returned (in opts.outWidth and opts.outHeight)`. – mrsus Jan 07 '14 at 09:17
  • 1
    for testing, you can use `BitmapFactory.decodeStream(is,null,null);` If image is decoded then it means BitmapFactory.Options are not suitable for image. Try to use different option settings. – mrsus Jan 07 '14 at 09:19
  • BitmapFactory.decodeStream(is,null,null); do return the bitmap, but I guess the issue is not that one. I used decodeStream twice on same inputstream, which is causing issue. If I remove the first decodeStream statement and put scale =1 or any number, the bitmap shows. Hence like you said I think I have to reset inputstream before decoding again. But for some reason I am not able to do that. – abdfahim Jan 07 '14 at 09:52

1 Answers1

-3

at

iv.setImageResource(resId);

resId is invalid value...

change it to

iv.setImageResource(R.drawable.ic_launcher);

and test the code

Taryn
  • 242,637
  • 56
  • 362
  • 405