0

Can anyone see what Im doing wrong here? I know it is a lot of code but I do not get any errors other then that the images do not turn up inside the imageviews. The imageviews turns up but empty.

I guess Im running the asynctask in the wrong place or something?

Here's the code:

MainActivity.java:

package se.bjornange.android.grid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;

public class mainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);    
    gridview.setAdapter(new ImageAdapter(this));
}

}

ImageAdapter.java

package se.bjornange.android.grid;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter implements NetworkCallListener {
private Context mContext;
Bitmap thebmp;
ImageView imageView;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return aThumbUrls.length;
}

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

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

public void onNetworkCallComplete(Bitmap bmp) {

    thebmp = bmp;

}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {


    new NetworkCallTask(this).execute(aThumbUrls[position]);

    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);

    } else {

        imageView = (ImageView) convertView;
    }

    imageView.setImageBitmap(thebmp);

    return imageView;
}

private String[] aThumbUrls = { "http://www.google.se/images/logo_sm.gif",
        "http://www.google.se/images/logo_sm.gif", "http://www.google.se/images/logo_sm.gif"
            };

@Override
public void onNetworkCallCancel() {
    // TODO Auto-generated method stub

}

}

NetworkCallListener.java

package se.bjornange.android.grid;

import android.graphics.Bitmap;

public interface NetworkCallListener {
    public void onNetworkCallComplete(Bitmap bmp); 

  public void onNetworkCallCancel();

}

NetworkCallTask.java

package se.bjornange.android.grid;


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

public class NetworkCallTask extends AsyncTask<String, Void, Bitmap> {
NetworkCallListener listener = null;
String url;
  public NetworkCallTask(NetworkCallListener listener) {
    this.listener = listener;
  }


protected void onPreExecute() {

}

protected Bitmap doInBackground(String... params) {

    Bitmap bmImg = null;
    url = params[0];
    URL myFileUrl = null;


    try {
        myFileUrl = new URL(url);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl
                .openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();

        bmImg =  BitmapFactory.decodeStream(is);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bmImg;
}

protected void onPostExecute(final Bitmap bmImg) {
     //imageView.setImageBitmap(bmImg);

    listener.onNetworkCallComplete(bmImg);
}

}
TMA-1
  • 84
  • 1
  • 9

2 Answers2

1

reefer below link

Lazy load of images in ListView

Source is available here http://open-pim.com/tmp/LazyList.zip

Community
  • 1
  • 1
Nikhil
  • 16,194
  • 20
  • 64
  • 81
  • Thanks a lot. I will take a look at it and try to change it to use a gridview. – TMA-1 May 13 '11 at 11:41
  • Yes I have, and Im currently struggeling with implementing it in my app. It seems to be what I was looking for but I have some nullpointer exceptions to solve =) Im a Android and Java rookie so sometimes I get stuck with the most basic stuff. =) – TMA-1 May 17 '11 at 06:53
  • i have try lasy list code and its working perfectly so can you elaborate your null pointer exceptions code. – Nikhil May 17 '11 at 07:13
  • Yep, I get it to work too as it is, but when I implement it in my app I don't get any images. I now have come so far I don't get any error and no crash but bitmap from ImageLoder returns null. – TMA-1 May 17 '11 at 18:20
  • Hi have you give below permission – Nikhil May 17 '11 at 18:48
  • The write external storage was missing. Now it works. SuperThanks! – TMA-1 May 17 '11 at 19:23
0

I don't think you can assign the bitmap after you set it with imageView.setImageBitmap(thebmp); You should do the imageView.setImageBitmap() in your onNetworkCallComplete() function;

Try something this:

    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {
      final ImageView imageV = convertView == null ? new ImageView(this) : (ImageView) convertView;
      new AsyncTask(){
        Bitmap bitmap;
        @Override
        protected Object doInBackground(Object... params) {
          bitmap = load your bitmap here
          return null;
        }
        @Override
        protected void onPostExecute(Object result) {
          imageV.setImageBitmap(bitmap);
        }
      }.execute(null);
      return imageV;
    }
Karl-Bjørnar Øie
  • 5,554
  • 1
  • 24
  • 30
  • I thought that was the problem too, but it made no difference. If feels like the loaded bitmap is not getting through to the imageview somewhere. – TMA-1 May 13 '11 at 11:21
  • Try to remove your toplevel ImageView declaration, and instead create a new ImageView each time in getView(), unless of course convertView != null, then pass the created or recycled ImageView to your NetworkCallTask and do the imageView.setImageBitmap() inside onPostExecute() – Karl-Bjørnar Øie May 13 '11 at 11:26
  • I tried to do something like you suggest before, but I couldnt find out how to get the imageView accessible from the NetworkCallTask. Any suggestion on that? – TMA-1 May 13 '11 at 11:35
  • I dont know how to put the code in the comment, so i have edited my answer with an example. – Karl-Bjørnar Øie May 13 '11 at 11:45