【问题标题】:I am trying to load image from server using asynctask in android but images don't get displayed.I don't know why?我正在尝试使用 android 中的 asynctask 从服务器加载图像,但图像没有显示。我不知道为什么?
【发布时间】:2017-03-10 13:04:08
【问题描述】:

我的代码是:

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

在模拟器上运行它时,我没有显示图像。帮我解决这个问题。我什至不知道 eroor 是什么?如果有的话。

【问题讨论】:

  • 尝试使用断点并调试代码以更好地了解问题所在。
  • 我检查了日志。它说“称为未实现的opengl es api”
  • @Garry Emulator 不支持 opengl 检查这里...stackoverflow.com/questions/5926316/…
  • 能否请您为您在 aSyncTask 中处理的异常记录日志。您可能会得到一些有趣的东西来解决您的问题。
  • 我试过你的代码,它在模拟器上运行良好,图像下载需要时间,可能是因为互联网连接。你可以尝试使用加载器,直到图像加载。

标签: android android-asynctask


【解决方案1】:

如果你想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);

   }
 }   

【讨论】:

  • 你能告诉我在给定代码中的确切位置吗?
  • 你需要在项目中添加库
  • 我添加了从这里下载的库:“square.github.io/picasso”,但它说 com.squareup 无法解析。
  • 试试android studio或者intellij idea,我不知道你做错了什么
【解决方案2】:

您可以使用 Picasso 和 Glide 库轻松下载图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 2018-05-13
    • 2019-08-08
    • 1970-01-01
    相关资源
    最近更新 更多