开发者

Loading external images to a gridview (no images)

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 {
 开发者_如何学Go       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);
}

}


reefer below link

Lazy load of images in ListView

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


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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜