从电话库显示空白图像的图像gridview

时间:2014-07-15 10:14:53

标签: android

我正在开发一个应用程序,我需要实现一个包含手机上所有图像的gridview。 我写了代码,但问题是,当我打开这个应用程序时,我得到一个可滚动的BLANK gridview,其中没有图像。 有人可以帮帮我吗?

这是我的主要活动代码:

package com.example.gridviewgallery;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.widget.GridView;

public class MainActivityGridView extends ActionBarActivity {

    private int columnIndex;
    private Cursor cursor = null;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity_grid_view);

        String[] projection = {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.DATA};
        cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, 
                MediaStore.Images.Thumbnails.IMAGE_ID);
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

        GridView gridview = (GridView) findViewById(R.id.gridView1);
        gridview.setAdapter(new GridAdapter(getApplicationContext(), cursor, columnIndex));

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这是我的GridAdapter类代码:

package com.example.gridviewgallery;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;

public class GridAdapter extends CursorAdapter
{
    private Context mContext;
    private Cursor mCursor;
    private int mColumnIndex;
    private ImageView imageView;

    @SuppressWarnings("deprecation")
    public GridAdapter(Context context, Cursor c, int ci) {
        super(context, c);
        mContext = context;
        mCursor = c;
        mColumnIndex = ci;
    }

    @Override
    public void bindView(View convertView, Context context, Cursor curs)
    {
        imageView = (ImageView) convertView.findViewById(R.id.image);
    }

    @Override
    public View newView(Context context, Cursor curs, ViewGroup parent)
    {
        imageView = new ImageView(mContext);

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.inflate(R.layout.grid_image, parent, false);

        int id = curs.getInt(mColumnIndex);
        imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(id)));
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);

        return retView;
    }

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

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

    public int mygetItemId(int position) {
        return 0;
    }

}

这是grid_image.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:orientation="vertical"
    android:padding="5dp" 
    android:clickable="true"
    android:focusable="true">

    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ImageView>

</LinearLayout>

感谢。

1 个答案:

答案 0 :(得分:0)

这不使用您的xml文件。 (不能等待;-)。)分配位图必须在bindView()中完成。

@Override
public void bindView(View view, Context context, Cursor curs)
{        
    ImageView imageView = (ImageView) view;
    int id = curs.getInt(mColumnIndex);
    imageView.setImageURI( Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
         String.valueOf(id)));
    imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setPadding(8, 8, 8, 8);
}

@Override
public View newView(Context context, Cursor curs, ViewGroup parent)
{
    return new ImageView(context);
}