如何从特定文件夹组成GridView图像列表

时间:2014-05-23 08:21:02

标签: android adapter android-gridview android-sdcard

我使用GridView我可以在行和列中显示我的图像。但在这种情况下,几乎所有图库中的图像都会出现在桌面上。我想要来自特定文件夹的图片,例如sdcard/Images/aaa.jpg。你可以建议我怎样才能在下面的代码中做到这一点?谢谢。

这是活动类:

public class CardBox extends Activity {

private Cursor cursor;
private int columnIndex;

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

String[] projection = {MediaStore.Images.Thumbnails._ID};

cursor = getContentResolver().query( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection, // Which columns to return
            null,       // Return all rows
            null,
            MediaStore.Images.Thumbnails._ID);


columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

GridView sdcardImages = (GridView) findViewById(R.id.gridview);
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_CENTER);
                    picturesView.setPadding(8,8,8,8);
                    picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
                }
                else {
                    picturesView = (ImageView)convertView;
                }
                return picturesView;
            }

} 

2 个答案:

答案 0 :(得分:0)

由于您知道所需的文件夹,您可以先在文件夹中迭代,然后在Collection中存储图像和存储路径,并在适配器中使用该列表来填充数据。

答案 1 :(得分:0)

以这种方式查询

private String[] arrPath;
    private boolean[] thumbnailsselection;
    private int ids[];
    private int count;

    final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
    final String orderBy = MediaStore.Images.Media._ID;

    Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, new String[] {"%myimagesfolder%"}, orderBy); //UPDATE HERE

    int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
    count = imagecursor.getCount();
    arrPath = new String[this.count];
    ids = new int[count];
    for (int i = 0; i < count; i++) {
        imagecursor.moveToPosition(i);
        ids[i] = imagecursor.getInt(image_column_index);
        int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        arrPath[i] = imagecursor.getString(dataColumnIndex);
    }