我的代码是:
public class MainActivity extends ActionBarActivity {
String URL1 = "http://cs619925.vk.me/v619925510/1b82b/0nTm-Pj0ABM.jpg";
String URL2 = "http://8tracks.imgix.net/i/000/955/740/87318.original-8382.jpg?rect=128,0,768,768&q=65&sharp=15&vib=10&fm=jpg&fit=max&w=200&h=200";
String URL3 = "http://a400.idata.over-blog.com/300x225/1/89/70/64/Autres-images/smiley-face-on-beach.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView myFirstImage = (ImageView) findViewById(R.id.iv1);
ImageView mySecondImage = (ImageView) findViewById(R.id.iv2);
ImageView myThirdImage = (ImageView) findViewById(R.id.iv3);
myFirstImage.setTag(URL1);
mySecondImage.setTag(URL2);
myThirdImage.setTag(URL3);
new DownloadImagesTask().execute(myFirstImage);
new DownloadImagesTask().execute(mySecondImage);
new DownloadImagesTask().execute(myThirdImage);
}
public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {
ImageView imageView = null;
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
Bitmap bmp =null;
try{
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
return bmp;
}catch(Exception e){}
return bmp;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在模拟器上运行时,我没有显示图像。帮助我这个。我甚至不知道这是什么?如果有的话。
答案 0 :(得分:0)
如果您想要download file或者您想要将图片加载到imageview,那么 使用picasso:
Picasso.with(context).load(URL1).into(myFirstImage);
public class MainActivity extends ActionBarActivity {
String URL1 = "http://cs619925.vk.me/v619925510/1b82b/0nTm-Pj0ABM.jpg";
String URL2 = "http://8tracks.imgix.net/i/000/955/740/87318.original-8382.jpg?rect=128,0,768,768&q=65&sharp=15&vib=10&fm=jpg&fit=max&w=200&h=200";
String URL3 = "http://a400.idata.over-blog.com/300x225/1/89/70/64/Autres-images/smiley-face-on-beach.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView myFirstImage = (ImageView) findViewById(R.id.iv1);
ImageView mySecondImage = (ImageView) findViewById(R.id.iv2);
ImageView myThirdImage = (ImageView) findViewById(R.id.iv3);
Picasso.with(this).load(URL1).into(myFirstImage);
Picasso.with(this).load(URL2).into(mySecondImage);
Picasso.with(this).load(URL3).into(myThirdImage);
}
}
答案 1 :(得分:0)
您可以使用Picasso和Glide库轻松下载图像。