2012-08-29 46 views
6

我正在從url下載圖像,但下載完成後圖像未更改。 我在下面輸入代碼,有人遇到過相同的問題嗎?從url上查看Android圖像

Java文件

public class MyImgActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ImageView imgView =(ImageView)findViewById(R.id.imageView1); 
    Drawable drawable = LoadImageFromWebOperations("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png"); 

    imgView.setImageDrawable(drawable); 
} 
private Drawable LoadImageFromWebOperations(String url) { 
    try 
     { 
     InputStream is = (InputStream) new URL(url).getContent(); 
     Drawable d = Drawable.createFromStream(is, "src name"); 
     return d; 
     }catch (Exception e) { 
     System.out.println("Exc="+e); 
     return null; 
     } 
} 
} 

XML文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"></ImageView> 
</LinearLayout> 

Manifest文件

<uses-permission android:name="android.permission.INTERNET"/> 
+2

你是什麼意思「改變圖像URL不顯示圖像」?你能改述一下嗎? –

+0

你有沒有嘗試加載jpg而不是png? – 2012-08-29 07:38:27

+0

我改變了「http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png」此網址,但不顯示圖像 – Kumar

回答

17

請使用下面的代碼下載並將圖像顯示到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"); 
     ImageView img = (ImageView) findViewById(R.id.img); 
     img.setImageBitmap(bitmap); 
    } 

    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

我正在嘗試此代碼但未顯示 – Kumar

+0

在logcat中顯示此消息08-29 14:04:53.046:D/dalvikvm(132):GC_EXPLICIT釋放了743個對象/ 42080 bytes in 163ms 08-29 14:04:58.096:D/dalvikvm(266):GC_EXPLICIT在182ms中釋放了362個對象/ 19720個字節 08-29 14:05:05。196:D/dalvikvm(291):GC_EXPLICIT在76ms中釋放了274個對象/ 12920個字節 08-29 14:09:21.957:​​D/SntpClient(59):請求時間失敗:java.net.SocketException:地址族不受支持協議 – Kumar

+0

請幫我一個代碼 – Kumar

3

下面的代碼使用下列URL工作,但它不是你的url.the問題是與工作你的圖片尺寸。試試看它的網址將工作。

public class MyImgActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ImageView imgView =(ImageView)findViewById(R.id.imageView1); 
     URL url = null; 
     Bitmap bmp = null; 
     try { 
      url = new URL("http://www.seobook.com/images/smallfish.jpg"); 
      bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
     } catch (MalformedURLException e) { 

     }catch (IOException e) { 

     } 
     imgView.setImageBitmap(bmp); 
     } 

    } 
+0

imgView.setImageURI(yourURL); yourURL是Uri只接受的類型?所以我更改了代碼,如Uri uri = Uri.parse(url) ; imgView.setImageURI(uri);,仍然沒有得到 – Kumar

+0

ok Kumar我編輯了這個工作的代碼。如果你的圖像超過了位圖限制,它將不會顯示。希望代碼沒有問題。你的問題與您的圖片 –

+0

感謝您的幫助代碼 – Kumar

0

嘗試下面的代碼,

只是改變您的網址,

URL

的onCreate

new Thread(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      drawable = LoadImageFromWebOperations("http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png"); 
      handler.sendEmptyMessage(0); 
     } 
    }).start(); 

使用下載圖像處理程序

後集圖像
Handler handler = new Handler(){ 
    @Override 
    public void handleMessage(android.os.Message msg) { 
     imgView.setImageDrawable(drawable); 
     Log.i("System out","after set the image..."); 
    } 
}; 

希望幫助你...

1
try 
    { 

    URL murl = new URL(url) 
    URLConnection ucon = murl.openConnection(); 
    InputStream is = ucon.getInputStream(); 
    Drawable d = Drawable.createFromStream(is, "src name"); 
    return d; 
    }catch (Exception e) { 
    System.out.println("Exc="+e); 
    return null; 
    } 

使用本COSE您的下載方式中,如果連接速度很慢使用線程donwload和hanlder張貼的圖片由@Hiren解釋