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

そして、私のグリッドビューの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 に含まれるすべての画像が表示されます...「content:///mnt/sdcard/folder/subfolder」のような URI パスを設定しようとしましたが、機能しません...どうすればよいですかMediaStore managedQuery で、スキャンする特定のフォルダーを指定しますか??

よろしくお願いします!:)

4

1 に答える 1

3

MediaStore.Images.Media.DATAフィールドを取得してみてください-画像ファイルへのフルパスが含まれています。次に、カーソルを実行して関心のあるファイルのみを選択するか、クエリを作成して、パスが「LIKE%your_folder_name%」のエントリのみを返すようにします。

于 2012-03-03T16:52:39.617 に答える