来自互联网的Gridview图像

时间:2012-11-26 18:59:37

标签: android

我正在尝试在Gridview中显示来自互联网的图像。当我运行我的程序时,catlog中没有错误,但我的屏幕上没有显示图像..

ImageAdapter.java:

public class ImageAdapter extends BaseAdapter {
private Context mContext;
Bitmap bmImg;
ImageView imageView;

public String[] mThumbIds = {
        "http://i724.photobucket.com/albums/ww246/bnhenson/squishy.jpg", "http://cdn4.teen.com/wp-content/uploads/2012/07/paranorman-zayn-malik-tweets.jpg",
        "http://xc0.xanga.com/08fe2b7a30c37281496919/m224298958.jpg"
};

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

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {         
    imageView = new ImageView(mContext);
    downloadFile(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(135, 135));
    imageView.setPadding(0, 0, 1, 0);
    return imageView;
}
void downloadFile(String fileUrl) {

    AsyncTask<String, Object, String> task = new AsyncTask<String, Object, String>() {

        @Override
        protected String doInBackground(String... params) {
            URL myFileUrl = null;
            try {
                myFileUrl = new URL(params[0]);
            } 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 null;
        }
        protected void onPostExecute(String unused) {
            imageView.setImageBitmap(bmImg);
        }
    };
    task.execute(fileUrl);

}}

AndroidGridLayoutActivity.java

public class AndroidGridLayoutActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_layout);

    GridView gridView = (GridView) findViewById(R.id.grid_view);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));
}}

grid_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >  

</GridView>

在我的清单中,我添加了这两行:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

有没有人有想法,我在这里错过了什么步骤?

先谢谢你的帮助,

Germain的。

1 个答案:

答案 0 :(得分:1)

您应该为网格的每个单元格创建一个单独的ImageView对象,而不是使用全局变量。要做到这一点,所以:

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext); 
    downloadFile(imageView, mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(135, 135));
    imageView.setPadding(0, 0, 1, 0);
    return imageView;
}

void downloadFile(ImageView imageView, String fileUrl) {
    // ...
    // change your AsyncTask so you can pass imageView as well
    // instead of setting loaded bitmap to global mImageView, set it to the passed in imageView.
    // ...
}

要更改AsyncTask,请将params的类型更改为Object,以便将imageViewfileUrl传递给doInBackground()或创建一个类使用构造函数的AyncTask,您可以传递两个对象并将它们存储在AsyncTask的本地变量中,以便您可以从doInBackground()方法访问它们。

另请注意,如果您的网格中有更多项目,那么当它具有非空值时,您应该重用传入的convertView