毕加索没有加载图片

时间:2015-04-21 01:05:06

标签: java android picasso

出于某种原因,每当我尝试将从GET请求获取的URL加载到服务器时,它都不会加载,但如果我尝试直接加载字符串,则可以正常工作。这是我的代码:

new Thread(new Runnable() {
        @Override public void run() {
            try {
                URL obj = new URL(url1 + overallid);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();

                con.setRequestMethod("GET");

                con.addRequestProperty("User-Agent", "Mozilla/4.76");

                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                System.out.println(response.toString().replaceAll("\\s", ""));
                System.out.println("Set pic to: " + pic);
                Picasso.with(LoginActivity.this).load(pic).into(image);
                i++;
                overallid++;
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }).start();

如果我直接将pic =一个imgur链接起作用,但是如果我从GET中获取它就不起作用。有什么想法吗?

谢谢, 昆(融合)

2 个答案:

答案 0 :(得分:1)

毕加索应该从主线程中调用...试试这段代码:

Handler mainThreadHandler=new Handler();
new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            URL obj = new URL(url1 + overallid);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            con.setRequestMethod("GET");

            con.addRequestProperty("User-Agent", "Mozilla/4.76");

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            System.out.println(response.toString().replaceAll("\\s", ""));
            System.out.println("Set pic to: " + pic);
            mainThreadHandler.post(new Runnable() {
                @Override
                public void run() {
                    Picasso.with(LoginActivity.this).load(pic).into(image);
                }
            });
            i++;
            overallid++;
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}).start();

答案 1 :(得分:1)

以下是处理位图下载的示例。

    BufferedInputStream inputStream = null;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        URL url = new URL("the image url to load");
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();

        inputStream = new BufferedInputStream(connection.getInputStream());

        byte[] buffer = new byte[8192];
        int n = -1;
        while ((n = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, n);
        }

        final Bitmap bitmap = BitmapFactory.decodeByteArray(
                outputStream.toByteArray(), 0, outputStream.size());

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO handle image here
            }
        });
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }