当我尝试从网址加载一个图片时,它正常工作,但是当我尝试加载两个图片时,它只是加载了最后一个图像。
这是我到目前为止所尝试的:
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 :(得分: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
设置为各自的位图。
答案 1 :(得分: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));
}
答案 2 :(得分: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();
}