Error displaying web images in java android

2

I'm trying to display web images in a ListView , but they appear out of order, in the wrong lines, and change positions with every Activity update.

Activity :

public class ListarProdutosActivity extends Activity {
    DataBaseHandler db = new DataBaseHandler(this);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] from = new String[] {
            DataBaseHandler.KEY_PRODUTOS_DESCRICAO, DataBaseHandler.KEY_PRODUTOS_VALOR, DataBaseHandler.KEY_PRODUTOS_IMAGE
        };
        int[] to = {
            R.id.txtDescricao, R.id.txtValor, R.id.logo
        };
        Cursor cursor = db.listarProdutos();

        @SuppressWarnings("deprecation")
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_produtos, cursor, from, to);

        ListView dataList = (ListView) findViewById(R.id.list);
        dataList.setAdapter(adapter);

        adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            /** Binds the Cursor column defined by the specified index to the specified view */
            public boolean setViewValue(View view, final Cursor cursor, final int columnIndex) {
                final ImageView imgView = (ImageView) view.findViewById(R.id.logo);

                if (view.getId() == R.id.logo) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Log.v("aviso", "image");
                                Drawable image = getImagem(cursor.getString(columnIndex));

                                imgView.setImageDrawable(image);
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }).start();

                    return true;
                }
                return false;
            }
        });
    }

    public Drawable getImagem(String image) throws Exception {
        URL url = new URL(image);

        InputStream is = (InputStream) getObjeto(url);
        Drawable d = Drawable.createFromStream(is, "src");

        return d;
    }

    private Object getObjeto(URL url) throws MalformedURLException, IOException {
        Object content = url.getContent();
        return content;
    }
}

Or if someone knows a better way to do this, I appreciate the suggestions.

    
asked by anonymous 20.10.2014 / 16:41

1 answer

4

There are libraries that load images synchronously or asynchronously, save the cached image and / or disk to save the band and abstract this part of the encoding.

Take a look at this Project:

link

EDIT

Improving the answer a little:

Android Universal Image Loader is a library that abstracts the entire image download portion, can be performed synchronously and asynchronously. In your code, it would look something like this:

public class ListarProdutosActivity extends Activity {
    DataBaseHandler db = new DataBaseHandler(this);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Configuração default do Image Loader
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
        ImageLoader.getInstance().init(config);

        String[] from = new String[] {
            DataBaseHandler.KEY_PRODUTOS_DESCRICAO, DataBaseHandler.KEY_PRODUTOS_VALOR, DataBaseHandler.KEY_PRODUTOS_IMAGE
        };
        int[] to = {
            R.id.txtDescricao, R.id.txtValor, R.id.logo
        };
        Cursor cursor = db.listarProdutos();

        @SuppressWarnings("deprecation")
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_produtos, cursor, from, to);

        ListView dataList = (ListView) findViewById(R.id.list);
        dataList.setAdapter(adapter);

        adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            /** Binds the Cursor column defined by the specified index to the specified view */
            public boolean setViewValue(View view, final Cursor cursor, final int columnIndex) {
                final ImageView imgView = (ImageView) view.findViewById(R.id.logo);

                if (view.getId() == R.id.logo) {
                   //Acredito que a URL seja obtida assim e a mesma deve ser enviada como string
                   URL url = new URL(cursor.getString(columnIndex));
                   //Aqui o ImageLoader fica responsável por obter a imagem na internet
                   ImageLoader.displayImage(url.toString() , imgView);
                    return true;
                }
                return false;
            }
        });
    }

    public Drawable getImagem(String image) throws Exception {
        URL url = new URL(image);

        InputStream is = (InputStream) getObjeto(url);
        Drawable d = Drawable.createFromStream(is, "src");

        return d;
    }

    private Object getObjeto(URL url) throws MalformedURLException, IOException {
        Object content = url.getContent();
        return content;
    }
}

I believe this basic example will work for you, after you've imported the library into your project. If you access the Universal ImageLoader link, you will find that there are several useful settings for your application! Hope it helps!

    
23.10.2014 / 05:53