Quando vc cria seu adapter, vc faz isso no seu getView:
URL aURL = new URL(myRemoteImages[position]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);bis.close();
is.close();
Mas isso vc fez dentro do método onCreate da sua activity, ou seja, no fim das contas vc tá abrindo uma conexão web dentro da thread principal.
Tenta modificar essa lógica de carregamento das imagens e botar isso dentro da sua ASyncTask.
2012/11/5 Felippe dos Santos Ferreira <felippenasuacasa@gmail.com>
amigo poderia demosntrar não entendi...2012/11/5 Bernardo Ferreira e Souza <bernardobfs@gmail.com>
--Você tá fazendo uma chamada à web dentro da sua thread principal.. e isso não pode..Separe a chamada em uma ASyncTask que resolve.2012/11/5 Felippe dos Santos Ferreira <felippenasuacasa@gmail.com>
galera mudei o codigo nesta madrugada...
ele fala de um erro na theads...
mais não consegui entender do que se trata o erro.AdaptadordaImagem adp = new AdaptadordaImagem(this);
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(adp);
}
}
___________________________________private Object mGalleryItemBackground;
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;PegaImagens pg = new PegaImagens();
public AdaptadordaImagem(Context c) {
cont = c;
pg.execute();
}
private TypedArray obtainStyledAttributes(int[] hellogallery) {return null;
// TODO Auto-generated method stub
}
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;int i;
}// aqui fecha o getView
public Bitmap loadBitmap(String[] myRemoteImages,
BitmapFactory.Options options) {
InputStream in = null;
for(i = 0; i <= 3; i++){
try {
in = OpenHttpConnection(myRemoteImages[i]);bm = null;
bm = BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (IOException e1) {
private InputStream OpenHttpConnection(String myRemoteImages) throws IOException {
}
}
return bm;
}// aqui fecha o load bitmap
InputStream inputStream = null;
URL url = new URL(myRemoteImages);public class PegaImagens extends AsyncTask<String[], Void, String> {
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 InputStreamprotected String doInBackground(String[]... params) {
private ProgressDialog progressgallery;
protected void onPreExecute() {
progressgallery = new ProgressDialog(cont, GalItemBg);
progressgallery.setTitle("Aguarde...");
progressgallery.setMessage("Carregando...");
progressgallery.show();
}int i;
// TODO Auto-generated method stub
for(i = 0; i <= 3; i++){
try{}catch(ArrayIndexOutOfBoundsException erro){
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
myRemoteImages[i]= "";
}catch(Exception erro){
myRemoteImages[i] = "";
}
}
loadBitmap(myRemoteImages, bmOptions);
return myRemoteImages[i];}TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
protected void onPostExecute(String[] myRemoteImages) {
if (!myRemoteImages.equals("")) {
// ok
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...
valewpublic 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
}
--






0 comentários:
Postar um comentário