Android: Bug with ThreadSafeClientConnManager downloading images

前端 未结 11 1468
我寻月下人不归
我寻月下人不归 2020-12-23 23:12

For my current application I collect images from different \"event providers\" in Spain.

  Bitmap bmp=null;
  HttpGet httpRequest = new HttpGet(strURL);

  l         


        
11条回答
  •  悲哀的现实
    2020-12-24 00:06

    My solution is not to use BitmapFactory.decodeStream() cause the way I look at it, it uses the SKIA decoder and it seems kind of erratic sometimes. You can try something like this.

        Bitmap bitmap = null;
        InputStream in = null;
        BufferedOutputStream out = null;
        try {
                in = new BufferedInputStream(new URL(url).openStream(),
                         IO_BUFFER_SIZE);
    
                final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
                out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
                copy(in, out);
                out.flush();
    
                final byte[] data = dataStream.toByteArray();
                bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    
    
       } catch (......){
    
       }        
    

    and for the copy() function

    private static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] b = new byte[IO_BUFFER_SIZE];
        int read;
        while ((read = in.read(b)) != -1) {
            out.write(b, 0, read);
        }
    }
    

    and IO_BUFFER_SIZE is a constant integer with the value of 4 * 1024.

提交回复
热议问题