2

ネットから何らかのURLから画像を取得し、ImageViewに表示したいと思います。以下は私が使用しているコードです:-

Bitmap bm = null;
    try {
         URL aURL = new URL("stringURL"); 
         URLConnection conn = aURL.openConnection();
         conn.connect(); 
         InputStream is = conn.getInputStream();
         BufferedInputStream bis = new BufferedInputStream(is);
         bm = BitmapFactory.decodeStream(bis);
         bis.close();
         is.close(); 
    }catch (Exception e) {
        // TODO: handle exception
}
    iv.setImageBitmap(bm);

しかし、私は画像を取得できません

4

2 に答える 2

0

エラーは

URL aURL = new URL("stringURL");

引用符なしである必要があります

URL aURL = new URL(stringURL);

stringURLが有効なURLである場合、それは機能します。

それが役に立てば幸い../

于 2011-03-25T09:02:35.737 に答える
0

以下のコードを使用する方が良いと思います。これは私にとって非常にうまく機能します。

String stringURL = "Your url here";

InputStream is = null;
BufferedInputStream bis = null;
Bitmap bmp = null;

try {
    URL url = new URL(stringURL);   
    URLConnection conn = url.openConnection();
    conn.connect();
    is = conn.getInputStream();
    bis = new BufferedInputStream(is);
    bmp = BitmapFactory.decodeStream(bis);

} catch (MalformedURLException e) {

} catch (IOException e) {

}catch (Exception e) {

} finally {
    try {
        if( is != null )
            is.close();
        if( bis != null )
            bis.close();
    } catch (IOException e) {

    }
}
iv.setImageBitmap(bmp);
于 2011-03-25T09:15:38.620 に答える