图像未显示在图像视图上

时间:2013-01-03 09:28:45

标签: android bitmap imageview

我正在尝试在imageview上设置图像,但图像未显示。

我正在从json数据中读取图像URL,然后尝试在ImageView上设置它,但我的图像不可见。没有任何例外。
这是我的代码

HotelList.class

static final String TAG_DISHIMAGEURL = "dishimageurl";
......
String imageUrl = dishResult.getString(TAG_DISHIMAGEURL);
map.put(TAG_DISHIMAGEURL, imageUrl);
.....
dishimageurl1 = hm.get(TAG_DISHIMAGEURL).toString();
 intent.putExtra("background", dishimageurl1);

HotelDetails.class

......
String dishimageurl = bundle.getString("background");
Bitmap bimage=  getBitmapFromURL(dishimageurl);
    imageView.setImageBitmap(bimage);
....   
public Bitmap getBitmapFromURL(String src) {
try {
    URL url = new URL(src);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    Toast.makeText(this, "showing image", Toast.LENGTH_LONG).show();
    connection.connect();
    InputStream input = connection.getInputStream();
    Bitmap myBitmap = BitmapFactory.decodeStream(input);
    return myBitmap;
} catch (IOException e) {
    Toast.makeText(this, "showing exception", Toast.LENGTH_LONG).show();
    return null;
}

}

我不明白这段代码会发生什么。没有任何例外,但我的图像不可见 请给我任何参考。

3 个答案:

答案 0 :(得分:1)

请使用以下代码从网址获取图片并显示到imageview。

public class image extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Bitmap bitmap = DownloadImage("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");

        RelativeLayout mRlayout1 = (RelativeLayout) findViewById(R.id.mRlayout1);
        Drawable d=new BitmapDrawable(bitmap);
        mRlayoutLogin.setBackgroundDrawable(d);
    }

    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;
    }
}

答案 1 :(得分:0)

您可以使用此代码查看图像。

    try {
    bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

答案 2 :(得分:0)

您似乎正在从UI线程下载图像。这将阻止UI线程,并将给你没有响应错误。作为一种简单的方法,您可以使用像Universal Image Loader这样的库

Universal Image Loader - GitHub

这将为您管理图像加载,并避免诸如错误的网址,内存不足错误等问题。