如何使用文件夹视图制作图库?

时间:2014-06-13 10:23:25

标签: android android-fragments image-gallery android-gallery

I want this kind of app。我想创建一个带有父文件夹视图的图库,我可以在网格视图中获取所有图像(媒体),但我想要图像的父文件夹视图。

以下是代码:

Cursor imagecursor = managedQuery(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
            null, orderBy);
    int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
    this.count = imagecursor.getCount();
    this.thumbnails = new Bitmap[this.count];
    this.arrPath = new String[this.count];
    this.thumbnailsselection = new boolean[this.count];
    for (int i = 0; i < this.count; i++) {
        imagecursor.moveToPosition(i);
        int id = imagecursor.getInt(image_column_index);
        int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
                getApplicationContext().getContentResolver(), id,
                MediaStore.Images.Thumbnails.MICRO_KIND, null);
        arrPath[i]= imagecursor.getString(dataColumnIndex);
    }

getView()

Adapter方法
public View getView(int position, View convertView, ViewGroup parent) 
{
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.list_item, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);


            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.imageview.setId(position);

        holder.imageview.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                int id = v.getId();
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
                startActivity(intent);
            }
        });
        holder.imageview.setImageBitmap(thumbnails[position]);

        holder.id = position;
        return convertView;
    }`

我这样做是为了获取SD卡中所有图像的网格视图。我如何建立一个文件夹系统,我可以点击父文件夹,然后打开图像?

1 个答案:

答案 0 :(得分:1)

试试这样..我希望这可以帮到你。

  Cursor imagecursor = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        columns, 
                        MediaStore.Images.Media.DATA + " like ? ",
                        new String[] {"%/yourfoldername/%"},  
                        null);