2013-10-25 141 views
0

當我試圖從url只加載一個圖像時,它正在工作,但是當我試圖加載兩個圖像時,它只是加載最後一張圖像。將兩個圖像從url加載到圖像查看

這是我到目前爲止已經試過:

 ProgressDialog pd; 
private ImageView imgView1, imgView2; 
    private String strURLJohnA = "sample1.jpg"; 
    private String strURLJohnR = "sample2.jpg"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.info); 

    back = (Button) findViewById (R.id.btBack); 
    back.setOnClickListener(this); 

    imgView1 = (ImageView) findViewById(R.id.ivInfo1); 
    imgView2 = (ImageView) findViewById (R.id.ivInfo2); 

    // Create an object for subclass of AsyncTask 
    GetXMLTask task = new GetXMLTask(); 
    // Execute the task 
    task.execute(new String[] { strURLJohnA, strURLJohnR }); 

} 

private class GetXMLTask extends AsyncTask<String, Void, Bitmap> { 
    @Override 
    protected Bitmap doInBackground(String... urls) { 
     Bitmap map = null; 
     for (String url : urls) { 
      map = downloadImage(url); 
     } 
     return map; 
    } 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
     pd = ProgressDialog.show(Info.this, "Please wait", "Downloading content", false, true); 
    } 

    // Sets the Bitmap returned by doInBackground 
    @Override 
    protected void onPostExecute(Bitmap result) { 
     imgView1.setImageBitmap(result); 
     imgView2.setImageBitmap(result); 
     pd.dismiss(); 
    } 

    // Creates Bitmap from InputStream and returns it 
    private Bitmap downloadImage(String url) { 
     Bitmap bitmap = null; 
     InputStream stream = null; 
     BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
     bmOptions.inSampleSize = 1; 

     try { 
      stream = getHttpConnection(url); 
      bitmap = BitmapFactory. 
        decodeStream(stream, null, bmOptions); 
      stream.close(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     return bitmap; 
    } 

    // Makes HttpURLConnection and returns InputStream 
    private InputStream getHttpConnection(String urlString) 
      throws IOException { 
     InputStream stream = null; 
     URL url = new URL(urlString); 
     URLConnection connection = url.openConnection(); 

     try { 
      HttpURLConnection httpConnection = (HttpURLConnection) connection; 
      httpConnection.setRequestMethod("GET"); 
      httpConnection.connect(); 

      if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       stream = httpConnection.getInputStream(); 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return stream; 
    } 
} 

任何想法如何解決這個問題?我非常感謝你的幫助。謝謝。

+0

你使用了一個位圖,其中首先包含圖像,然後在與第二位圖寫道。你需要一個位圖數組來做到這一點。 –

回答

2

(String... urls)是Var args。你不需要傳遞一個字符串數組。使用如下面的代碼:

task.execute(strURLJohnA, strURLJohnR); 


private class GetXMLTask extends AsyncTask<String, Void, ArrayList<Bitmap>> { 
    @Override 
    protected ArrayList<Bitmap> doInBackground(String... urls) { 
     ArrayList<Bitmap> map = new ArrayList<Bitmap>(); 

      map.add(downloadImage(urls[0]));// I used as this for you to understand. You can use for each loop 
      map.add(downloadImage(urls[1])); 

     return map; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<Bitmap> result) { 
     imgView1.setImageBitmap(result.get(0)); 
     imgView2.setImageBitmap(result.get(1)); 

    } 
4

你一個列表,而不是如下:

private class GetXMLTask extends AsyncTask<String, Void, ArrayList<Bitmap>> { 
    @Override 
    protected ArrayList<Bitmap> doInBackground(String... urls) { 
     ArrayList<Bitmap> map = new ArrayList<Bitmap>(); 
     for (String url : urls) { 
      map.add(downloadImage(url)); 
     } 
     return map; 
    } 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
     pd = ProgressDialog.show(Info.this, "Please wait", "Downloading content", false, true); 
    } 

    // Sets the Bitmap returned by doInBackground 
    @Override 
    protected void onPostExecute(ArrayList<Bitmap> result) { 
     imgView1.setImageBitmap(result.get(0)); 
     imgView2.setImageBitmap(result.get(1)); 
     pd.dismiss(); 
    } 
... 
... 
... 

您需要返回Bitmaplist一個對象,如我在代碼中顯示上方。然後將imageView設置爲其各自的位圖。

0
 ProgressDialog pd; 
    private ImageView imgView1, imgView2; 
     //we can store any number of urls in a string array and load them at a time 
    private String[] str={"http://www.bogotobogo.com/images/rodin.jpg","http://www.bogotobogo.com/images/smalltiger.gif"}; 

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

    imgView1=(ImageView)findViewById(R.id.imageView1); 
    imgView2=(ImageView)findViewById(R.id.imageView2); 


    // Create an object for subclass of AsyncTask 
    GetXMLTask task = new GetXMLTask(); 
    // Execute the task 
    task.execute(str); 
} 


private class GetXMLTask extends AsyncTask<String, Void, ArrayList<Bitmap>> { 
@Override 
protected ArrayList<Bitmap> doInBackground(String... urls) { 
    ArrayList<Bitmap> map = new ArrayList<Bitmap>(); 

    //enhanced for statement, mainly used for arrays 
    for (String url : urls) { 
     map.add(downloadImage(url)); 
    } 
    return map; 
} 

@Override 
protected void onPreExecute() { 
    // TODO Auto-generated method stub 
    super.onPreExecute(); 
    pd = ProgressDialog.show(Info.this, "Please wait", "Downloading content", false, true); 
} 

// Sets the Bitmap returned by doInBackground 
@Override 
protected void onPostExecute(ArrayList<Bitmap> result) { 
    imgView1.setImageBitmap(result.get(0)); 
    imgView2.setImageBitmap(result.get(1)); 
    pd.dismiss(); 
}