在MediaStore managedQuery中指定一个文件夹

时间:2012-03-03 14:42:50

标签: android sd-card mediastore

您好我想在SD卡上指定一个文件夹来检索该文件夹的子文件夹中的所有图像(例如,/ mnt / sdcard / Folder / SubFolder)。我是这样的:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery);
    GridView gridview = (GridView) findViewById(R.id.gallery);
    registerForContextMenu(gridview);

    id = getIntent().getExtras().getString("ID");

    // 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的setAdapter中我有:

gridview.setAdapter(new BaseAdapter() {
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView picturesView;
            if (convertView == null) {
                picturesView = new ImageView(mContext);
                // 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;
        }

        public int getCount() {
            return cursor.getCount();
        }

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

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

    });

这是有效的,但显示了SD卡中包含的所有图像...... 我试图设置一个URI路径,比如“content:/// mnt / sdcard / folder / subfolder”,但它不起作用......如何在MediaStore managedQuery中指定要扫描的特定文件夹?

提前谢谢大家! :)

1 个答案:

答案 0 :(得分:3)

尝试获取MediaStore.Images.Media.DATA字段 - 它包含图像文件的完整路径。然后,您可以通过光标运行并仅选择您感兴趣的文件或构建查询,这样它只返回路径为“LIKE%your_folder_name%”的条目