0

当我尝试从 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;
    }
}

任何想法如何解决这个问题?非常感谢您的帮助。谢谢。

4

3 回答 3

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();
    }
...
...
...

您需要返回我在上面的代码中显示list的对象。Bitmap然后将 设置imageView为其各自的位图。

于 2013-10-25T11:01:58.733 回答
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));

    }
于 2013-10-25T11:06:29.720 回答
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();
}
于 2014-03-12T11:14:11.633 回答