List View with TextView and ImageView

2

I'm trying to make a ListView with TextView and ImageView (obtained through a URL), when I was only with TextView was appearing normal, but when I added the code to the images, the screen all white. What could be the problem?
I used the setImageURI() method to set the image link. the code I'm using is as follows:

  public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    StockQuoteView sqView = null;

    if(rowView == null)
    {
        // Get a new instance of the row layout view
        LayoutInflater inflater = activity.getLayoutInflater();
        rowView = inflater.inflate(R.layout.noticia_list_item, null);

        // Hold the view objects in an object,
        // so they don't need to be re-fetched
        sqView = new StockQuoteView();
         //sqView.quote = (TextView) rowView.findViewById(R.id.ticker_symbol);
        sqView.ticker = (TextView) rowView.findViewById(R.id.ticker_price);
        sqView.img  = (ImageView) rowView.findViewById(R.id.img);     
        // Cache the view objects in the tag,
        // so they can be re-accessed later
        rowView.setTag(sqView);
    } else {
        sqView = (StockQuoteView) rowView.getTag();
    }

    // Transfer the stock data from the data object
    // to the view objects
    Noticias currentStock = (Noticias) stocks.get(position);
    sqView.ticker.setText(currentStock.getTitulo());
    sqView.img.setImageURI(currentStock.getImagem());// bom aqui eu pensei que o setImageURI iria setar o link da imagem e assim exibi-la... mas é necessário uma conversão pra bitmap?

  // sqView.quote.setText(currentStock.getDescricao());

    return rowView;
    
asked by anonymous 13.01.2015 / 18:09

1 answer

2

Do not confuse URL with URI. You can not request data from a URL, for example, www.meusite.com.br/imagens/imagem.png , via setImageURI() .

This is one thing [search the image on your device via the path given in the URI]:

Uri imgUri=Uri.parse("file:///data/data/MINHA_PASTA_ANDROID/minhaimage.png");
imgview.setImageUri(imgUri);

And this is something else [Converts the Bitmap image of a URL]:

private Bitmap getImageBitmap(String url) {
    Bitmap bm = null;
    try {
        URL aURL = new URL(url);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
   } catch (IOException e) {
       Log.e(TAG, "Error getting bitmap", e);
   }
   return bm;
} 
    
13.01.2015 / 19:01