android - 获取特定文件夹的 MediaStore 路径

标签 android gridview mediastore android-ion

我正在为我的应用程序创建一个更新,其中有一个文件夹,其中包含我想在 GridView 中显示的已保存图像。我已经在使用 Ion library .库的创建者 (Koush Dutta) 已经有一个样本可以做我想做的事并显示 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
);

关于android - 获取特定文件夹的 MediaStore 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30407581/

相关文章:

android - 显示没有文本Android的进度对话框

android - 将自定义指标附加到 Android Analytics v4 中的 HitBuilders.TransactionBuilder

android - jmethodID 的 NewGlobalRef

c# - IE8 固定页眉,可滚动的 GridView

php - 在 yii2 gridview 中输入时进行过滤

android - 获取歌曲流派的最有效方法 android

android - 使用 json post 从移动应用程序中使用 Devise 注册帐户

css - GridView 页码出现在相距很远的单独列中

android - 无法在 Android 10 上更新 MediaStore

java - MediaStore 返回 NULL