0

您好我想在 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;
        }

    });

这行得通,但向我显示 sdcard 中包含的所有图像...我尝试设置一个 URI 路径,例如“content:///mnt/sdcard/folder/subfolder”,但它不起作用...我怎么能在 MediaStore managedQuery 中指定要扫描的特定文件夹?

谢谢大家!:)

4

1 回答 1

3

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

于 2012-03-03T16:52:39.617 回答