获取特定文件夹的 MediaStore 路径 [英] Get MediaStore path of a Specific Folder

查看:25
本文介绍了获取特定文件夹的 MediaStore 路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序创建更新,在其中我有一个文件夹,其中包含我想在 GridView 中显示的已保存图像.我已经在使用 Ion 库.库的创建者 (Koush Dutta) 已经有一个样本做我想做的事情,并显示了 GridView 中的 SD 卡..

I'm creating an update for my application, in it I have a folder with saved images that I would like to display in a GridView. I am already using the 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 卡上特定文件夹(称为漫画)中的图像.我直接使用上面示例中的代码,只修改了一些名称.

What I want to do is display only the images from a specific folder on my SD Card (called comics) in the GridView. I am using the code directly from the sample above, only modifying some names.

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

My Problem is that I cannot get only the images from the SD Card into the GridView, currently it gets all the images from the SD Card. I want only from the folder /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);
        }
    }
}

我尝试了一些方法,其中一个是像这个答案:

I have tried a few things, one being something like this answer:

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

推荐答案

我刚刚在 这个答案.

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

Turns out all you need to do is use this code below to get the cursor, and it will only load images from the /sdcard/comics/ folder.

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

Note that I tested this with the Ion library and your code, and it worked for me!

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

这篇关于获取特定文件夹的 MediaStore 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆