服务器检索到的图像没有显示在android中的对话框中

时间:2013-12-02 21:11:52

标签: android bitmap dialog

好吧,我创建了一个对话框,将图像放在他的内部。 我从服务器上获取了图像。但是这个图像没有显示在Dialog上。

下面有一些错误?我必须提出什么样的解决方案?

有人能帮帮我吗?

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int posicao, long id) {
    String nome_item = (String) lista.getItemAtPosition(posicao);

    Bitmap img = DownloadImage("myUrl");

    dialogo = new Dialog(MainActivity.this);
    dialogo.setContentView(R.layout.dialogo);

    ImageView imgAlimento = (ImageView) dialogo.findViewById(R.id.imgTeste);
    imgAlimento.setImageBitmap(img);

    dialogo.setTitle(nome_item);

    Button bt = (Button) dialogo.findViewById(R.id.btnPedido);
    bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dialogo.dismiss();
        }
    });

    dialogo.show();

}

private InputStream OpenHttpConnection(String urlString) throws IOException {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))
        throw new IOException("Not an HTTP connection");

    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
        }
    } catch (Exception ex) {
        throw new IOException("Error connecting");
    }
    return in;

}

private Bitmap DownloadImage(String URL)
{        
    Bitmap bitmap = null;
    InputStream in = null;        
    try {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return bitmap;                
}

0 个答案:

没有答案