将外部图像加载到网格视图(无图像)

发布于 2024-11-07 00:56:37 字数 3997 浏览 5 评论 0原文

谁能看到我在这里做错了什么吗?我知道这是很多代码,但除了图像没有出现在图像视图内之外,我没有收到任何错误。图像视图出现但为空。

我想我在错误的地方运行异步任务还是什么?

代码如下:

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵性 2024-11-14 00:56:37

我不认为你可以在使用 imageView.setImageBitmap(thebmp); 设置位图后分配位图。您应该在 onNetworkCallComplete() 函数中执行 imageView.setImageBitmap() ;

尝试这样的事情:

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

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文