【问题标题】:Get Image from Urls with AsyncTask使用 AsyncTask 从 Url 获取图像
【发布时间】:2016-11-15 19:18:45
【问题描述】:

我确信这不是一个新问题。不幸的是,在为这项活动争取了 20 多个小时后,我仍然找不到解决方案。你能帮我一把吗?我将不胜感激。

嘿,我的问题。我想将来自不同 url 的图像显示到 girdView 布局。但是,我的代码不起作用,因此屏幕什么也没有显示。大佬能帮我解决一下吗?

下面是我的 MainActivity 类

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

    String [] imgUrlList = {
            "http://image.tmdb.org/t/p/w500/cGOPbv9wA5gEejkUN892JrveARt.jpg",
            "http://image.tmdb.org/t/p/w500/6FxOPJ9Ysilpq0IgkrMJ7PubFhq.jpg",
            "http://image.tmdb.org/t/p/w500/z09QAf8WbZncbitewNk6lKYMZsh.jpg",
            "http://image.tmdb.org/t/p/w500/kqjL17yufvn9OVLyXYpvtyrFfak.jpg"
    };


    mMovieAdapter = new MyAdapter(this, imgUrlList);
    GridView gridview = (GridView) findViewById(R.id.gridView);
    gridview.setAdapter(mMovieAdapter);

}

public class MyAdapter extends ArrayAdapter<String>{
    private final Activity context;
    private final String[] imgUrlList;

    public MyAdapter (Activity context, String[] imgUrlList){
        super(context, R.layout.image_item);
        this.context = context;
        this.imgUrlList = imgUrlList;
    }

    public View getView (int position, View view, ViewGroup parent){
        LayoutInflater inflater = context.getLayoutInflater();
        View rootView = inflater.inflate(R.layout.image_item, null, true);

        new DownloadImageTask((ImageView) rootView.findViewById(R.id.imageItem)).execute(imgUrlList[position]);
        ImageView imageView = (ImageView) rootView.findViewById(R.id.imageItem);
        imageView.setImageURI(Uri.parse(imgUrlList[position]));

        return rootView;
    }
}

private class DownloadImageTask extends AsyncTask <String, Void, Bitmap>{

    ImageView poster;

    public DownloadImageTask(ImageView poster) {
        this.poster = poster;
    }

    protected Bitmap doInBackground(String... urls){
        String imageUrls = urls[0];
        Bitmap moviePoster = null;
        try {
            InputStream in = new java.net.URL(imageUrls).openStream();
            moviePoster = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return moviePoster;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        poster.setImageBitmap(result);
    }
}

下面是我的 xml (activity_main) 文件

<GridView
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnWidth="185dp"
    android:numColumns="auto_fit"/>

我还有一个 imageView 文件调用 image_item

【问题讨论】:

  • 你应该考虑使用像 Glide/picasso/UIL 这样的库,它会更好地加载图像
  • 不要重新发明轮子。使用毕加索或类似的图书馆
  • 使用Picasso
  • 使用滑翔。易于使用和加载图像:这是链接:github.com/bumptech/glide
  • 参考this

标签: java android javers


【解决方案1】:

老实说,我不知道为什么。但是使用这个构造函数可以解决这个问题:

 super(context,0,imgUrlList);

而不是

super(context, R.layout.image_item);

【讨论】:

  • 坦率地说,我不太喜欢你的代码风格。到处都是小错误。
  • 感谢您的回复。我刚开始学习编码 10 天前。我知道有很多事情可以改进。但是,我尝试使用您提供的代码。但仍然无法在我的手机上工作。
  • 能告诉我如何找出这些小错误吗?
【解决方案2】:

您可以使用picaso 库来实现您的目的。 https://github.com/square/picasso

你只需要初始化 Picasso 类并设置 url。

【讨论】:

  • 是的,用毕加索之后,现在就好了~~谢谢!!
猜你喜欢
  • 2016-10-02
  • 2012-12-29
  • 2012-09-16
  • 2017-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
相关资源
最近更新 更多