注册 登录
编程论坛 Android开发

BitmapFactory.decodeByteArray返回值为null

xinzhong417 发布于 2012-08-03 15:30, 3649 次点击
从javaweb获取一张图片的数据流,方法如下:
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        PrintWriter writer = response.getWriter();
        InputStream inputStream = this.getServletContext().getResourceAsStream("/WEB-INF/pics/4.jpg");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String str;
        while((str=bufferedReader.readLine())!=null){
            writer.write(str);
            System.out.println("str="+str);
        }
        writer.flush();
        writer.close();
然后android客户端获取此数据流,
public static ByteArrayOutputStream readParse(String urlPath) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int len = 0;
        URL url = new URL(urlPath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        Log.i("xin", "conn="+conn);
        InputStream inStream = conn.getInputStream();
        
        while ((len = inStream.read(data)) != -1) {
            outStream.write(data, 0, len);

        }
        inStream.close();
        return outStream;

    }
然后调用上面的方法:
            ByteArrayOutputStream readParse = readParse(urlPath);
            byte[] byteArray = readParse.toByteArray();
            Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
却获得bitmap 为null,经验证从服务器端获取到了数据,byteArray 不为空。
求大神指点一下
0 回复
1