BitmapFactory.decodeStream always returns null and skia decoder shows decode returned false

后端 未结 9 1272
北恋
北恋 2020-12-09 06:50

test image here: http://images.plurk.com/tn_4134189_bf54fe8e270ce41240d534b5133884ee.gif

I\'ve tried several solutions found on the internet but there is no working

相关标签:
9条回答
  • 2020-12-09 07:49

    Try this:

    HttpGet httpRequest = new HttpGet(url);
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);
    InputStream is = bufferedHttpEntity.getContent();
    Drawable d = Drawable.createFromStream(is, "");
    //or bitmap
    //Bitmap b = BitmapFactory.decodeStream(is);
    
    0 讨论(0)
  • 2020-12-09 07:53

    this is due to a bug in the InputStream class in Android. You can find a valid workaround and a description of the bug here http://code.google.com/p/android/issues/detail?id=6066

    0 讨论(0)
  • 2020-12-09 07:53

    For memory reasons, you must be implements BitmapFactory options like this:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4; // might try 8 also
    

    The main download bitmap function maybe like this:

    Bitmap downloadBitmap(String url) {
    
        final HttpClient client = AndroidHttpClient.newInstance("Android");
        final HttpGet getRequest = new HttpGet(url);
    
        try {
            HttpResponse response = client.execute(getRequest);
            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                if(DEBUG)Log.w("ImageDownloader", "Error " + statusCode +
                        " while retrieving bitmap from " + url);
                return null;
            }
    
            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
    
                    inputStream = entity.getContent();
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 4; // might try 8 also
                    return BitmapFactory.decodeStream(new FlushedInputStream(inputStream),null,options);
    
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    entity.consumeContent();
                }
            }
        } catch (IOException e) {
            getRequest.abort();
            if(DEBUG)Log.w(TAG, "I/O error while retrieving bitmap from " + url, e);
        } catch (IllegalStateException e) {
            getRequest.abort();
            if(DEBUG)Log.w(TAG, "Incorrect URL: " + url);
        } catch (Exception e) {
            getRequest.abort();
            if(DEBUG)Log.w(TAG, "Error while retrieving bitmap from " + url, e);
        } finally {
            if ((client instanceof AndroidHttpClient)) {
                ((AndroidHttpClient) client).close();
            }
        }
        return null;
    }
    

    And maybe you must be implements AsyncTask like this: http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

    0 讨论(0)
提交回复
热议问题