将外部图像加载到网格视图(无图像)
谁能看到我在这里做错了什么吗?我知道这是很多代码,但除了图像没有出现在图像视图内之外,我没有收到任何错误。图像视图出现但为空。
我想我在错误的地方运行异步任务还是什么?
代码如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
冷藏以下链接
延迟加载图像ListView
源代码可在此处获取 http://open-pim.com/tmp/LazyList .zip
reefer below link
Lazy load of images in ListView
Source is available here http://open-pim.com/tmp/LazyList.zip
我不认为你可以在使用 imageView.setImageBitmap(thebmp); 设置位图后分配位图。您应该在 onNetworkCallComplete() 函数中执行 imageView.setImageBitmap() ;
尝试这样的事情:
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: