Converting input stream into bitmap

后端 未结 2 1857
终归单人心
终归单人心 2020-12-05 01:50

I have problems converting a input stream from web into bitmap. Problem occurs only when input image type is .BMP (bitmap). In that case: bitmapFactory.decodeStream

相关标签:
2条回答
  • 2020-12-05 02:43

    Thank you @Amir for point out the log. Discovered a line:

    decoder->decode returned false
    

    This seems to be a common problem. Doing a search I found a solution.

    My previous code:

    URLConnection conn = url.openConnection();
    conn.connect();
    
    inputStream = conn.getInputStream();
    
    bufferedInputStream = new BufferedInputStream(inputStream);
    
    bmp = BitmapFactory.decodeStream(bufferedInputStream);
    

    Code which is working:

    HttpGet httpRequest = null;
    
    try {
        httpRequest = new HttpGet(url.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    
    HttpClient httpclient = new DefaultHttpClient();
    
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
    
    HttpEntity entity = response.getEntity();
    
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    
    InputStream instream = bufHttpEntity.getContent();
    
    bmp = BitmapFactory.decodeStream(instream);
    

    Source

    0 讨论(0)
  • 2020-12-05 02:46

    Here is a one-line answer

    val bitmap = BitmapFactory.decodeStream(inputStream)
    

    Returns a Bitmap

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