从SD卡中获取图像并在Gridview中显示

时间:2012-02-27 21:54:18

标签: java android android-sdcard android-gridview

我试图从SD卡中获取所有图像并将其显示在gridview中(包含在片段中)。但是,虽然没有抛出任何异常,但gridview中只显示任何内容,只是一个普通的黑屏。我不确定问题出在哪里,视图绑定或将数据提取到光标中。这是片段的当前代码:

public class PhotoGridFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

// member variables for
private static final int PHOTO_LIST_LOADER = 0x01;
private ImageCursorAdapter adapter;
private Cursor c;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getLoaderManager().initLoader(PHOTO_LIST_LOADER, null, this);

    adapter = new ImageCursorAdapter(getActivity().getApplicationContext(), c);
}

/* R.layout.grid_item,
null, new String[] { MediaStore.Images.Thumbnails.DATA }, new int[] {R.id.grid_item},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); */

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.photo_item, container, false);     
}


// Loader manager methods
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = { MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.DATA };
    CursorLoader cursorLoader = new CursorLoader(getActivity(),
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
            null, null, null);
    return cursorLoader;
}

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    adapter.swapCursor(cursor);

}

public void onLoaderReset(Loader<Cursor> cursor) {
    adapter.swapCursor(null);
}

private class ImageCursorAdapter extends CursorAdapter {

    private LayoutInflater mLayoutInflater;
    private Context mContext;

    public ImageCursorAdapter(Context context, Cursor c) {
        super(context, c);
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ImageView newView = (ImageView) view.findViewById(R.layout.grid_item);
        String imagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
        if (imagePath != null && imagePath.length() != 0 && newView != null) {
            newView.setVisibility(ImageView.VISIBLE);
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = mLayoutInflater.inflate(R.layout.grid_item, parent, false);
        return v;
    }


}

项目的布局文件如下:

photo_item.xml:

<?xml version="1.0" encoding="UTF-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/photo_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="6dp" />

grid_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="6dp"

/>

2 个答案:

答案 0 :(得分:1)

在bindView中,你实际上并没有将imageView的drawable设置为任何东西。你抓住一个图像路径,验证它是一个真正的路径,然后忽略它:)使用路径获得一个drawable!然后将imageView的drawable设置为该图像。

答案 1 :(得分:1)

试试此代码

sdcard.java

public class Sdcard extends Activity {

//  Cursor used to access the results from querying for images on the SD card.

private Cursor cursor;

 // Column index for the Thumbnails Image IDs.

private int columnIndex;

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

    // Set up an array of the Thumbnail Image ID column we want
    String[] projection = {MediaStore.Images.Thumbnails._ID};
    // Create the cursor pointing to the SDCard
    cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection, // Which columns to return
            null,       // Return all rows
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);
    // Get the column index of the Thumbnails Image ID
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    GridView sdcardImages = (GridView) findViewById(R.id.gridView1);
    sdcardImages.setAdapter(new ImageAdapter(this));


}

private class ImageAdapter extends BaseAdapter {

    private Context context;

    public ImageAdapter(Context localContext) {
        context = localContext;
    }

    public int getCount() {
        return cursor.getCount();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView picturesView;
        if (convertView == null) {
            picturesView = new ImageView(context);
            // Move cursor to current position
            cursor.moveToPosition(position);
            // Get the current value for the requested column
            int imageID = cursor.getInt(columnIndex);
            // Set the content of the image based on the provided URI
            picturesView.setImageURI(Uri.withAppendedPath(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
            picturesView.setScaleType(ImageView.ScaleType.FIT_XY);
            picturesView.setPadding(10, 10, 10, 10);
            picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
        }
        else {
            picturesView = (ImageView)convertView;
        }
        return picturesView;
    }
}

}