2012-08-23 78 views
4

可能重複:
Loading remote images安卓:從網頁URL加載圖像

我開發一個應用程序,我在其中的每一行都有一個圖像列表,

此圖片是從網址加載的。下面

是我的代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
      android:id="@+id/list_row_img" 
      android:layout_width="200dp" 
      android:layout_height="200dp" 
      /> 

    <TextView 
      android:id="@+id/list_row_description" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 

</LinearLayout> 

,這是活動代碼

@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ImageView img= (ImageView) findViewById(R.id.list_row_img); 
    Bitmap bm = null; 
    try { 
     URL aURL = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464"); 
     URLConnection conn = aURL.openConnection(); 
     conn.connect(); 
     InputStream is = conn.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is); 
     bm = BitmapFactory.decodeStream(bis); 
     img.setImageBitmap(bm); 
     bis.close(); 
     is.close(); 

     } catch (Exception e) { 
      Log.v("EXCEPTION", "Error getting bitmap", e); 
     } 
} 

,當我跑我得到的設備上什麼都沒有。只有一個灰色的屏幕,並沒有發生異常。

注意,我在清單文件

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

加入此權限任何人都可以幫我嗎?

回答

7

使用下面的代碼下載圖像從URL和顯示圖像到ImageView的exeecute它,這將解決您的問題。

import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLConnection; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.widget.ImageView; 

public class MainActivity extends Activity { 

    ImageView mImgView1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
       .permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 

     mImgView1 = (ImageView) findViewById(R.id.mImgView1); 
     String url = "https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg"; 
     BitmapFactory.Options bmOptions; 
     bmOptions = new BitmapFactory.Options(); 
     bmOptions.inSampleSize = 1; 
     Bitmap bm = loadBitmap(url, bmOptions); 
     mImgView1.setImageBitmap(bm); 
    } 

    public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) { 
     Bitmap bitmap = null; 
     InputStream in = null; 
     try { 
      in = OpenHttpConnection(URL); 
      bitmap = BitmapFactory.decodeStream(in, null, options); 
      in.close(); 
     } catch (IOException e1) { 
     } 
     return bitmap; 
    } 

    private static InputStream OpenHttpConnection(String strURL) 
      throws IOException { 
     InputStream inputStream = null; 
     URL url = new URL(strURL); 
     URLConnection conn = url.openConnection(); 

     try { 
      HttpURLConnection httpConn = (HttpURLConnection) conn; 
      httpConn.setRequestMethod("GET"); 
      httpConn.connect(); 

      if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       inputStream = httpConn.getInputStream(); 
      } 
     } catch (Exception ex) { 
     } 
     return inputStream; 
    } 
} 
+0

感謝您的幫助。我有一個問題,請。 OpenHttpConnection(URL)此行有錯誤 是否有需要導入的API或庫? –

+0

@RanaO​​sama不,不需要任何API或庫。 –

+0

@RanaO​​sama請看我更新的導入包的答案。 –

2

您圖片的鏈接無效。嘗試使用瀏覽器訪問它,你會發現。那是你的問題。

+1

+1實際上,籤鏈接 – Gautam

+0

@高塔姆ķ@Lazy忍者鏈接有效載荷的圖像「雖然它沒有標記的圖像的圖像」 –

+0

的鏈接工作。你有沒有遇到過任何問題? @拉齊? –

10

試試這個 您可以通過

 new DownloadImageTask((ImageView) im1).execute(url here); 

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
    ImageView bmImage; 

    public DownloadImageTask(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     Bitmap mIcon11 = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mIcon11 = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return mIcon11; 
    } 


/* @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
     showProgressDialog(); 
    }*/ 

    protected void onPostExecute(Bitmap result) { 
     //pDlg.dismiss(); 
     bmImage.setImageBitmap(result); 
    }} 
+0

@Rana Osama嘗試了此代碼? –

+0

還沒有,但現在就可以了...非常感謝你的幫助=) –

+0

我用它。它很棒!謝謝。 – Ton