0

I have a issue in the method doInBackground of Scraping after get the link of the picture I also call other AsyncTask for downloading an bitmap of this. but not working when I put the bitmap in my ImageView.

Activity class:

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

Adapter class:

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

Scraping class

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 class:

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

help please.

  • have you checked to see if scraping.getImagen() is returning a valid bitmap object – faljbour Mar 16 '15 at 03:11
  • also look at download_bitmap_thumbnail method, you are calling download.execute() then download.getBitmap(), try to verify if the bitmap has been downloaded yet you may want to wait until the bitmap has been downloaded before you call download.getBitmap() – faljbour Mar 16 '15 at 03:25
  • Hi, ok, I will revise tonight. Thanks. – Android Developer Mar 16 '15 at 13:54
  • Hi, the method getImage is returning value empty; it is probably because the method getImage is called while the AsyncTask downloading is in the method doBackground . But I do not want this. I need that the image the title and description is loaded together. – Android Developer Mar 17 '15 at 01:26
  • see this post for waiting for the asynctask to finish, after calling execute, I think that might solve your issue. http://stackoverflow.com/questions/14827532/waiting-till-the-async-task-finish-its-work – faljbour Mar 17 '15 at 01:31

1 Answers1

0

Thanks you helped me, finally I solved the issue looking this post.

Make AsyncTask wait for other AsyncTasks.

I changed this code fragment :

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

for:

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

:)

Community
  • 1
  • 1