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

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

问题描述

我正在为我的应用程序创建一个更新,其中我有一个保存有图像的文件夹,我想在GridView中显示。我已经在使用离子库。图书馆的创建者(Koush Dutta)已经有了一个样本,可以做我想做的,并显示来自 GridView中的SD卡。

我想要做的是仅显示来自GridView中SD卡上特定文件夹(称为漫画)的图像。



我的问题是我无法获取来自上述示例的图片,只能修改某些名称。 SD卡插入到GridView中,目前它可以从SD卡获取所有图像。我只想从文件夹/ sdcard / Comics /



代码

  public class SavedComics extends Activity {
private MyAdapter mAdapter;

//适配器从包含在数组适配器中的URL填充和映射视图
private class MyAdapter extends ArrayAdapter< String> {
public MyAdapter(Context context){
super(context,0);
}

@Override
public View getView(int position,View convertView,ViewGroup parent){
//查看是否需要加载更多才能获得40,否则填充适配器
if(position> getCount() - 4)
loadMore();

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

//找到图片视图
final ImageView iv =(ImageView)convertView.findViewById(R.id.comic);

//选择图像视图
Ion.with(iv)
.centerCrop()
.error(R.drawable.error)
。负载(的getItem(位置));

返回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();
}

光标mediaCursor;
public void loadMore(){
if(mediaCursor == null){
mediaCursor = getContentResolver()。query(MediaStore.Files.getContentUri(external),null,null,null , 空值);
}

int loaded = 0;
(mediaCursor.moveToNext()&&&&& 10){
//获取媒体类型。 ion可以显示常规图像和视频的图像。
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;
}

加载++;

String uri = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));
档案档案=新档案(uri);
//把它变成一个文件uri if必要/可能
if(file.exists())
mAdapter.add(file.toURI()。toString());
else
mAdapter.add(uri);
}
}
}

我尝试了几件事,其中之一是像这样的答案

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


解决方案

我只是在这个答案



结果你需要做的就是使用下面的代码来获得游标,它只会从 / sdcard / comics / 文件夹。



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

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


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..

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.

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/

Code

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

解决方案

I just figured it out with help from this answer.

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.

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天全站免登陆