如何在我的图库应用中创建文件夹?

时间:2011-04-27 11:55:37

标签: android gallery directory

我创建了一个图库应用程序,它的工作正常。如果我运行图库显示所有SD卡图像 但是如何从特定文件夹中获取图片?我需要更改查询吗? 示例:壁纸,图像,相机,相册等

我的源代码:

package image.Thumbnails;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class ImageThumbnailsActivity extends Activity {
  /** Called when the activity is first created. */
  private Cursor imagecursor, actualimagecursor;
  private int image_column_index, actual_image_column_index;
  GridView imagegrid;
  private int count;
  @Override
  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init_phone_image_grid();
  }
  private void init_phone_image_grid() {
        String[] img = { MediaStore.Images.Thumbnails._ID };
        imagecursor = managedQuery(
  MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
  null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
        image_column_index = imagecursor
  .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
        count = imagecursor.getCount();
        imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
        imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
        imagegrid.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView parent, View v,
   int position, long id) {
                    System.gc();
                    String[] proj = { MediaStore.Images.Media.DATA };
                    actualimagecursor = managedQuery(
   MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
   null, null, null);
                    actual_image_column_index = actualimagecursor
   .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    actualimagecursor.moveToPosition(position);
                    String i = actualimagecursor.getString(actual_image_column_index);
                    System.gc();
                    Intent intent = new Intent(getApplicationContext(),    
    ViewImage.class);
                    intent.putExtra("filename", i);
                    startActivity(intent);
              }
        });
      }


    public class ImageAdapter extends BaseAdapter {
        private             Context mContext;
        public ImageAdapter(Context c) {
              mContext = c;
        }
        public int getCount() {
              return count;
        }
        public Object getItem(int position) {
              return position;
        }
        public long getItemId(int position) {
              return position;
        }
        public View getView(int position,View convertView,ViewGroup parent) {
              System.gc();
              ImageView i = new ImageView(mContext.getApplicationContext());
              if (convertView == null) {
                    imagecursor.moveToPosition(position);
                    int id = imagecursor.getInt(image_column_index);
                    i.setImageURI(Uri.withAppendedPath(
       MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
       + id));
                    i.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    i.setLayoutParams(new GridView.LayoutParams(92, 92));
              }
              else {
                    i = (ImageView) convertView;
              }
              return i;
        }
       }
         }

2 个答案:

答案 0 :(得分:0)

以下是Android图库应用源代码的链接:
https://android.googlesource.com/platform/packages/apps/Gallery3D

您可以将此作为应用的示例或起点。

答案 1 :(得分:0)