Tecnologia do Blogger.
RSS

[androidbrasil-dev] Re: Gallery com imagens da Web...

galera mudei o codigo nesta madrugada...
ele fala de um erro na theads...
mais não consegui entender do que se trata o erro.


public class ExibeGallery extends SherlockActivity {
    private Gallery galleryView1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exibegallery);
       
        AdaptadordaImagem adp =  new AdaptadordaImagem(this);
        galleryView1 = (Gallery) findViewById(R.id.GaleryView);
        galleryView1.setAdapter(adp);

    }
}
___________________________________

public class AdaptadordaImagem extends BaseAdapter {
    private Context cont;
    int GalItemBg;
    private String[] myRemoteImages = {
            "http://www.cuboweb.com.br/android/images/1.png",
            "http://www.cuboweb.com.br/android/images/2.png",
            "http://www.cuboweb.com.br/android/images/3.png",
            "http://www.cuboweb.com.br/android/images/4.png", };
   
    static Bitmap bm;
    BitmapFactory.Options bmOptions;
    private Object mGalleryItemBackground;

    public AdaptadordaImagem(Context c) {
        cont = c;
       
        PegaImagens pg = new PegaImagens();
        pg.execute();       
    }


    private TypedArray obtainStyledAttributes(int[] hellogallery) {
        // TODO Auto-generated method stub
        return null;
    }


    public int getCount() {
        return this.myRemoteImages.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(this.cont);
        try {
            URL aURL = new URL(myRemoteImages[position]);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            Bitmap bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
            i.setImageBitmap(bm);
        } catch (IOException e) {
            i.setImageResource(R.drawable.sem_conexao);
        }
        return i;
    }// aqui fecha o getView

    public Bitmap loadBitmap(String[] myRemoteImages,
            BitmapFactory.Options options) {
        InputStream in = null;
        int i;
        for(i = 0; i <= 3; i++){
            try {
                in = OpenHttpConnection(myRemoteImages[i]);
                bm = BitmapFactory.decodeStream(in, null, options);
                in.close();
            } catch (IOException e1) {
                bm = null;
            }
        }
        return bm;
    }// aqui fecha o load bitmap


    private InputStream OpenHttpConnection(String myRemoteImages) throws IOException {
        InputStream inputStream = null;
        URL url = new URL(myRemoteImages);
        URLConnection conn = url.openConnection();
        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = httpConn.getInputStream();
            }
        } catch (Exception ex) {
        }
        return inputStream;
    }// aqui fecha o InputStream

    public class PegaImagens extends AsyncTask<String[], Void, String> {
        private ProgressDialog progressgallery;

        protected void onPreExecute() {
            progressgallery = new ProgressDialog(cont, GalItemBg);
            progressgallery.setTitle("Aguarde...");
            progressgallery.setMessage("Carregando...");
            progressgallery.show();
        }

        protected String doInBackground(String[]... params) {
            // TODO Auto-generated method stub
            int i;
            for(i = 0; i <= 3; i++){
                try{
                        bmOptions = new BitmapFactory.Options();
                        bmOptions.inSampleSize = 1;                   
                }catch(ArrayIndexOutOfBoundsException erro){
                        myRemoteImages[i]= "";
                }catch(Exception erro){
                        myRemoteImages[i] = "";
                }
            }   
            loadBitmap(myRemoteImages, bmOptions);
            return myRemoteImages[i];
        }
       
        protected void onPostExecute(String[] myRemoteImages) {
            if (!myRemoteImages.equals("")) {
                // ok
                TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
                mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
                a.recycle();
                 
            } else {
                Log.i("onpost execute"," dentro do else de erro");
            }
            progressgallery.dismiss();
        }
    }// end async task
}// end class
_____________________________________________________

erros:

android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
at java.net.InetAddress.getAllByName(InetAddress.java:249)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)






Em sábado, 3 de novembro de 2012 22h39min35s UTC-3, Felippe dos Santos Ferreira escreveu:

ola galera
tou fazendo uma gallery... mais essa galery vai usar imagens que estãso na web...
mais não consigo arrumar um erro protected String doInBackground quando retorna o endereço daqs imagens...
se alguem puder dar uma força;;;.. se aguem jah fez algo assimm...

valew

public class AdaptadordaImagem extends BaseAdapter {
    private Context cont;
    int GalItemBg;
    private String[] myRemoteImages = {
            "http://www.cuboweb.com.br/android/images/1.png",
            "http://www.cuboweb.com.br/android/images/2.png",
            "http://www.cuboweb.com.br/android/images/3.png",
            "http://www.cuboweb.com.br/android/images/4.png", };
    static Bitmap bm;
    BitmapFactory.Options bmOptions;

    public AdaptadordaImagem(Context c) {
        cont = c;
   }

    public int getCount() {
        return this.myRemoteImages.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(this.cont);
        try {
            /* Definindo a URL das imagens. */
            URL aURL = new URL(myRemoteImages[position]);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();// pegando a imagem
            /* Create Buffered is always good for a performance plus. */
            BufferedInputStream bis = new BufferedInputStream(is);
            /* Transformando a imagem em bitmap. */
            Bitmap bm = BitmapFactory.decodeStream(bis);
            /**/
            bis.close();
            is.close();
            /* Apply the Bitmap to the ImageView that will be returned. */
            i.setImageBitmap(bm);
        } catch (IOException e) {
            i.setImageResource(R.drawable.sem_conexao);
            Log.e("DEBUGTAG", "Remtoe Image Exception", e);
        }
        return i;
    }// aqui fecha o getView

    public Bitmap loadBitmap(String myRemoteImages,
            BitmapFactory.Options options) {
        InputStream in = null;
        try {
            in = OpenHttpConnection(myRemoteImages);
            bm = BitmapFactory.decodeStream(in, null, options);
            in.close();
        } catch (IOException e1) {
        }
        return bm;
    }// aqui fecha o load bitmap

    private InputStream OpenHttpConnection(String strURL) throws IOException {
        InputStream inputStream = null;
        URL url = new URL(strURL);
        URLConnection conn = url.openConnection();

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = httpConn.getInputStream();
            }
        } catch (Exception ex) {
        }
        return inputStream;
    }// aqui fecha o InputStream

    public class PegaImagens extends AsyncTask<String, Void, String> {
        private ProgressDialog progressgallery;

        protected void onPreExecute() {
            progressgallery = new ProgressDialog(cont, GalItemBg);
            progressgallery.setTitle("Aguarde...");
            progressgallery.setMessage("Carregando...");
            progressgallery.show();
        }

        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 1;
            loadBitmap(myRemoteImages, bmOptions); //error

            return myRemoteImages;  //error
        }

        // esse metodo vai ver se as imagens estão acessiveis na internet
        protected void onPostExecute(String myRemoteImages) {
            if (!myRemoteImages.equals("")) {
                // ok
            } else {
                // aqui devo fazer carregar a imagen do cache
                // error
            }
            progressgallery.dismiss();
        }
    }// end async task
}// end class

______________________________________




public class ExibeGallery extends SherlockActivity {
    private Gallery galleryView1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exibegallery);

        galleryView1 = (Gallery) findViewById(R.id.GaleryView);
        galleryView1.setAdapter(new AdaptadordaImagem(this));

    }// end o oncreate

}

--
 
 

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comentários:

Postar um comentário