图像未在ImageView中显示

时间:2015-03-16 02:39:38

标签: imageview jsoup

我在scinping的方法doInBackground中有一个问题,在得到图片的链接后我也调用其他AsyncTask来下载这个的位图。但是当我将位图放在我的ImageView中时无效。

活动类:

 ListView listView = (ListView) findViewById(R.id.listScraping);
    ArrayList<Scraping> myList = new ArrayList<Scraping>();
    myAdapter = new MyAdapter(getApplicationContext(),myList);
    myList.add(new Scraping("https:  //www.youtube.com/watch?v=HMUDVMiITOU",myAdapter,myList));
    listView.setAdapter(myAdapter);

适配器类:

public class MyAdapter extends BaseAdapter {

    private Context ctx;
    private ArrayList<Scraping> arrayList;
    private LayoutInflater mInflater;

    public MyAdapter(Context ctx,ArrayList<Scraping> arrayList)
    {
        this.arrayList = arrayList;
        this.ctx = ctx;
        this.mInflater = LayoutInflater.from(ctx);
    }

    @Override
    public int getCount() {
        return this.arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        Scraping scraping = this.arrayList.get(position);

        if(convertView == null)
        {
            Log.d("CREATE","view "+ position);
            holder = new ViewHolder();
            convertView = this.mInflater.inflate(R.layout.preview_scraping,parent,false);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tituloScrap);
            holder.tvDescription = (TextView) convertView.findViewById(R.id.descripcionScrap);
            holder.ivThumbnail   = (ImageView) convertView.findViewById(R.id.thumbnailScrap);
            convertView.setTag(holder);
        }
        else
        {
            Log.d("EXISTS","view "+ position);
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvTitle.setText(scraping.getTitle()+" :: " + position);
        holder.tvDescription.setText(scraping.getDescription()+" :: " + position);
        holder.ivThumbnail.setImageBitmap(scraping.getImagen());

        return convertView;
    }

    private  class ViewHolder
    {
        TextView tvTitle;
        TextView tvDescription;
        ImageView ivThumbnail;
    };
}

刮痧课

public class Scraping {

    private String url;
    private String title;
    private String description;
    private Bitmap imagen;
    private MyAdapter adapter;
    private String titleScraping;
    private String descriptionScraping;
    private Bitmap imagenScraping;
    private ArrayList<Scraping> arrayList;

    public Scraping(String url,MyAdapter adapter,ArrayList<Scraping> arrayList)
    {
        this.url = url;
        this.adapter = adapter;
        this.arrayList = arrayList;
        scraping_url();
    }

    public Scraping(String title, String description) {
        this.title = title;
        this.description = description;
    }

    public Scraping(String title, String description,Bitmap imagen) {
        this.title = title;
        this.description = description;
        this.imagen = imagen;
    }

    public Bitmap getImagen() {
        return imagen;
    }

    public void setImagen(Bitmap imagen) {
        this.imagen = imagen;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void scraping_url()
    {
        final String urlStr = this.url;

        new AsyncTask<Void,Void,Void>()
        {
            @Override
            protected Void doInBackground(Void... params) {

                try {

                     Document doc = Jsoup.connect(urlStr).userAgent("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10").referrer("http://www.google.com").get();

                    String word_google  = "google";
                    String word_twitter = "twitter";

                    String title , description , image ;
                    image  = "";

                    title = doc.title();

                    if(title.equals(""))
                    {
                        title= doc.select("meta[property=og:title]").attr("content");
                    }

                    description  = doc.select("meta[name=description]").attr("content");

                    if(description.equals(""))
                    {
                        description= doc.select("meta[name=keywords]").attr("content");
                    }

                    if(description.equals(""))
                    {
                        description= doc.select("meta[property=og:description]").attr("content");
                    }

                    if(description.equals(""))
                    {
                        description = title;
                    }

                    Elements src_img = doc.select("img[src~=(?i)\\.(png|jpe?g)]");

                    if(src_img.size() > 0 )
                    {
                        image = src_img.first().attr("content");
                    }

                    if(image.equals(""))
                    {
                        image = doc.select("meta[property=og:image]").attr("content");
                    }

                    if(image.equals(""))
                    {
                        src_img = doc.select("link[href~=(?i)\\.(ico)]");
                        if(src_img.size() > 0 )
                        {
                            if(urlStr.contains(word_twitter) && image.equals(""))
                            {
                                image = src_img.first().attr("href");
                            }
                            else
                            {
                                image = urlStr + src_img.first().attr("href");
                            }
                        }
                    }

                    if(urlStr.contains(word_google) && image.equals(""))
                    {
                        image = urlStr + "/images/google_favicon_128.png";
                    }

                    titleScraping = title;
                    descriptionScraping = description;

                    if(image.length() > 0)
                    {
                        imagenScraping = download_bitmap_thumbnail(image);
                    }

                    return  null;

                }catch (IOException e)
                {
                    e.printStackTrace();
                    return null;
                }
            }

            @Override
            protected void onPostExecute(Void voids) {

                setTitle(titleScraping);
                setDescription(descriptionScraping);
                setImagen(imagenScraping);
                adapter.notifyDataSetChanged();
            }
        }.execute();
    }


    public Bitmap download_bitmap_thumbnail(String thumbnail)
    {
        //set image
        DownloadingImage download = new DownloadingImage(thumbnail);
        download.execute();
        return download.getBitmap();
    }
}

DownloadingImage类:

public class DownloadingImage extends AsyncTask<String, Void, Bitmap>
{
    private String url;
    private Bitmap bitmap;

    public DownloadingImage(String url)
    {
        this.url = url;
    }

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

    protected void onPostExecute(Bitmap result)
    {
        setBitmap(result);
    }

    public Bitmap getBitmap() {
        return bitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }
}
请帮忙。

1 个答案:

答案 0 :(得分:0)

谢谢你帮助了我,最后我解决了这个帖子的问题。

Make AsyncTask wait for other AsyncTasks

我更改了此代码片段:

public Bitmap download_bitmap_thumbnail(String thumbnail)
{
    //set image
    DownloadingImage download = new DownloadingImage(thumbnail);
    download.execute();
    return download.getBitmap();
}

有:

public void download_bitmap_thumbnail(String thumbnail)
{
    //set image
    DownloadingImage download = new DownloadingImage(thumbnail);
    setImagen(download.doInBackground());
}

:)