获取特定文件夹的 MediaStore 路径

Get MediaStore path of a Specific Folder

我正在为我的应用程序创建一个更新,其中有一个文件夹,其中包含我想在 GridView 中显示的已保存图像。我已经在使用 Ion library. The creator of the library (Koush Dutta) already has a sample doing what I want and displays all of the images from the SD Card in a GridView.

我想要做的是在 GridView 中仅显示我的 SD 卡上特定文件夹(称为漫画)中的图像。我直接使用上面示例中的代码,只修改了一些名称。

我的问题 是我不能只从 SD 卡获取图像到 GridView 中,目前它从 SD 卡获取所有图像。我只想从文件夹 /sdcard/Comics/

代码

public class SavedComics extends Activity {
    private MyAdapter mAdapter;

    // Adapter to populate and imageview from an url contained in the array adapter
    private class MyAdapter extends ArrayAdapter<String> {
        public MyAdapter(Context context) {
            super(context, 0);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // see if we need to load more to get 40, otherwise populate the adapter
            if (position > getCount() - 4)
                loadMore();

            if (convertView == null)
                convertView = getLayoutInflater().inflate(R.layout.image, null);

            // find the image view
            final ImageView iv = (ImageView) convertView.findViewById(R.id.comic);

            // select the image view
            Ion.with(iv)
            .centerCrop()
            .error(R.drawable.error)
            .load(getItem(position));

            return convertView;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.gallery);

        int cols = getResources().getDisplayMetrics().widthPixels / getResources().getDisplayMetrics().densityDpi * 2;
        GridView view = (GridView) findViewById(R.id.results);
        view.setNumColumns(cols);
        mAdapter = new MyAdapter(this);
        view.setAdapter(mAdapter);

        loadMore();
    }

    Cursor mediaCursor;
    public void loadMore() {
        if (mediaCursor == null) {
            mediaCursor = getContentResolver().query(MediaStore.Files.getContentUri("external"), null, null, null, null);
        }

        int loaded = 0;
        while (mediaCursor.moveToNext() && loaded < 10) {
            // get the media type. ion can show images for both regular images AND video.
            int mediaType = mediaCursor.getInt(mediaCursor.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE));
            if (mediaType != MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
                && mediaType != MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO) {
                continue;
            }

            loaded++;

            String uri = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));
            File file = new File(uri);
            // turn this into a file uri if necessary/possible
            if (file.exists())
                mAdapter.add(file.toURI().toString());
            else
                mAdapter.add(uri);
        }
    }
}

我尝试了一些东西,其中一个是 like this answer:

getContentResolver().query(
                   uri1, Selection, 
                   android.provider.MediaStore.Audio.Media.DATA + " like ? ", 
                   new String[] {str}, null);

我只是在 this answer 的帮助下弄明白了。

原来你需要做的就是使用下面的代码获取光标,它只会从 /sdcard/comics/ 文件夹加载图像。

请注意,我使用 Ion 库和您的代码对此进行了测试,它对我有效!

mediaCursor = getContentResolver().query(
    MediaStore.Files.getContentUri("external"),
    null,
    MediaStore.Images.Media.DATA + " like ? ",
    new String[] {"%comics%"},
    null
);