0

我正在尝试从 SD 卡中获取所有图像并将它们显示在网格视图中(包含在片段中)。然而,虽然没有抛出异常,但是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"

/>
4

2 回答 2

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

}

于 2012-12-13T11:05:47.613 回答
1

在 bindView 中,您实际上并没有将 imageView 的可绘制对象设置为任何内容。您抓取图像路径,验证它是真实路径,然后忽略它:) 使用路径获取可绘制对象!然后将 imageView 的可绘制对象设置为该图像。

于 2012-02-27T22:18:21.520 回答