Get Image from Urls with AsyncTask

谁说我不能喝 提交于 2019-12-25 07:14:04

问题


I am sure this is not a new question. Unfortunately, after fighting for this activity over 20 hours, I'm still cannot find out the solution. Could you please give me a helping hand. I will be very appreciate.

Hey is my question. I would like to show Image from different urls to a girdView layout. However, my code didn't work so the screen show nothing. Could you guy please help me to solve this?

Below is my MainActivity Class

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

Below is my xml (activity_main) file

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

I also have a imageView file call image_item


回答1:


Honestly, I don't know why. But use this constructor fix this problem:

 super(context,0,imgUrlList);

instead of

super(context, R.layout.image_item);



回答2:


You may use picaso library to server your purpose. https://github.com/square/picasso

You just need to initialize Picasso class and set url.



来源:https://stackoverflow.com/questions/38347670/get-image-from-urls-with-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!