Converting InputStream into Image

0

I need to download an image and save it to my device. I download it and write to InputSstream

InputStream imagem = comunicacao.getWSStream(servico, nome);

But at the time of converting it, decodebyteArray ends up generating a null.

 private void gravaImagem(InputStream imagem, String nome){
    try {
        FileOutputStream escrita = new FileOutputStream(new File(SivaPlusConfig.DIRETORIO_SDIMG + nome));
        byte[] bytes = readBytes(imagem);
        Bitmap imagem2 = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); // gera null
        if (imagem2 == null) {
            return;
        }
        imagem2.compress(Bitmap.CompressFormat.PNG, 90, escrita);
        escrita.flush();
        escrita.close();
        imagem.close();
    } catch (Exception e){
        LogUtil.writeFile((Activity) mContext, "Erro ao gravar imagem: " + nome);
    }
}

 private byte[] readBytes(InputStream in) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) > 0) {
            bos.write(buffer, 0, len);
        }
        byte[] bytes = bos.toByteArray();
        return bytes;
    } finally {
        bos.close();
    }
}

Sorry for the text formatting. I'm still adapting and learning about the community.

    
asked by anonymous 20.07.2017 / 15:40

1 answer

0

Finished. It was webservice problem, "Content-Type" was different than expected. Thanks for the help.

    
20.07.2017 / 20:52