如何根据android中本机相机拍摄的照片动态更新图像库?

如何根据android中本机相机拍摄的照片动态更新图像库?,android,android-camera,image-gallery,Android,Android Camera,Image Gallery,这是我的图片库代码。到目前为止,出于测试目的,我直接使用了一些手动存储的图像 public class Photo_Gallery extends Activity { //---the images to display--- Integer[] imageIDs = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e

这是我的图片库代码。到目前为止,出于测试目的,我直接使用了一些手动存储的图像

public class Photo_Gallery extends Activity
{    
//---the images to display---
Integer[] imageIDs = {
        R.drawable.a,
        R.drawable.b,
        R.drawable.c,
        R.drawable.d,
        R.drawable.e,
};

@Override    
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.displayview);

    Gallery gallery = (Gallery) findViewById(R.id.gallery1);

    gallery.setAdapter(new ImageAdapter(this));        
    gallery.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView parent, View v, int position, long id) 
                {                
                    //---display the images selected---
                    ImageView imageView = (ImageView) findViewById(R.id.image1);                
                    imageView.setImageResource(imageIDs[position]);
                }
    });
}

public class ImageAdapter extends BaseAdapter 
{
    private Context context;
    private int itemBackground;

    public ImageAdapter(Context c) 
    {
        context = c;
        //---setting the style---
        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
        itemBackground = a.getResourceId(
            R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();                    
    }

    //---returns the number of images---
    public int getCount() {
        return imageIDs.length;
    }

    //---returns the ID of an item--- 
    public Object getItem(int position) {
        return position;
    }            

    public long getItemId(int position) {
        return position;
    }

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(imageIDs[position]);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(new Gallery.LayoutParams(75, 60));
        imageView.setBackgroundResource(itemBackground);
        return imageView;
    }
}    
}

这是我用来捕捉图像的代码

    case R.id.takeSnapBtn:

        try {

            i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, imageData);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
以下是onActivityResult()


如果有人能给我举个例子,告诉我如何用相机拍摄的图像动态更新图库,我会很高兴的。

存储从应用程序捕获的图像,并将这些图像从sd卡读取到图库

File sdDir = new File("/sdcard/Pictures/YOUR_DIR");
sdDirFiles = sdDir.listFiles();
for (i = 0; i < sdDirFiles.length; i++) {
//ADD to gallery HERE..
}
File sdDir=新文件(“/sdcard/Pictures/YOUR_DIR”);
sdDirFiles=sdDir.listFiles();
对于(i=0;i
Bucket表示存储图像的文件夹名称。获取相应的文件夹/存储桶图像,或者使用我提供的代码获取所有图像。
File sdDir = new File("/sdcard/Pictures/YOUR_DIR");
sdDirFiles = sdDir.listFiles();
for (i = 0; i < sdDirFiles.length; i++) {
//ADD to gallery HERE..
}
If u get all images under the sdcard of perticular Bucket(folder) then use this code...
public class SubGalleryView extends Activity {

    private ArrayList<Integer> ImageIds = new ArrayList<Integer>();
    private GridView subGalleryView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sub_gallery_view);


            //Provide Bucket_Name/Folder_Name Using Bundle.

        Bundle bundle = getIntent().getExtras();
        Toast.makeText(this, bundle.getString("Bucket_Name"),
                Toast.LENGTH_SHORT).show();

            //Using Projection We get all images of all folder.
        String[] projection = new String[] { MediaStore.Images.Media._ID,
                MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME };
        Cursor cursor = managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
                null, null);
        cursor.moveToFirst();
        if (cursor.getCount() > 0) {
                    //using this do{}while(); we get all image_id under perticular bucket. and strored in ArrayList of ImagesIds.
            do {
                int columnIndex = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME);
                if (cursor.getString(columnIndex).equalsIgnoreCase(
                        bundle.getString("Bucket_Name"))) {
                    columnIndex = cursor
                            .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
                    int imageID = cursor.getInt(columnIndex);
                    ImageIds.add(imageID);
                } else if (bundle.getString("Bucket_Name").equalsIgnoreCase(
                        "All Pictures")) {
                    columnIndex = cursor
                            .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
                    int imageID = cursor.getInt(columnIndex);
                    ImageIds.add(imageID);
                }
            } while (cursor.moveToNext());
        }

            //Pass ImageIds arrayList to Adapter.
        subGalleryAdapter imageAdapter = new subGalleryAdapter(
                SubGalleryView.this, R.layout.sub_gallery_view_item, ImageIds);
        subGalleryView = (GridView) findViewById(R.id.subGallery);
        subGalleryView.setAdapter(imageAdapter);
    }

    public class subGalleryAdapter extends ArrayAdapter<Integer> {

        private Context contex;
        private int resourceId;
        private LayoutInflater layoutInflater = null;
        private ArrayList<Integer> mlist;

        public subGalleryAdapter(Activity context, int resourceId,
                ArrayList<Integer> list) {
            super(context, resourceId, list);
            this.mlist = list;
            this.contex = context;
            this.resourceId = resourceId;
            this.layoutInflater = context.getLayoutInflater();

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView viewHolder;
            if (convertView == null) {
                viewHolder = new ImageView(contex);
                convertView = layoutInflater.inflate(resourceId, parent, false);
                viewHolder = (ImageView) convertView
                        .findViewById(R.id.imageView1);

            } else {
                viewHolder = (ImageView) convertView
                        .findViewById(R.id.imageView1);
            }
            viewHolder.setImageURI(Uri.withAppendedPath(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    "" + mlist.get(position)));
            return convertView;
        }

    }
}
Using this code u get all images from perticular folder passing bucket display name in bundle. and if u get all images of all bucket(folder) form sdcard then use this code
public class SubGalleryViewy extends Activity {


private Cursor cursor;

private int columnIndex;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sdcard);

    // Set up an array of the Thumbnail Image ID column we want
    String[] projection = {MediaStore.Images.Thumbnails._ID};
    // Create the cursor pointing to the SDCard
    cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection, // Which columns to return
            null,       // Return all rows
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);
    // Get the column index of the Thumbnails Image ID
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
    sdcardImages.setAdapter(new ImageAdapter(this));

    // Set up a click listener
    sdcardImages.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            // Get the data location of the image
            String[] projection = {MediaStore.Images.Media.DATA};
            cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    projection, // Which columns to return
                    null,       // Return all rows
                    null,
                    null);
            columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToPosition(position);
            // Get image filename
            String imagePath = cursor.getString(columnIndex);
            // Use this path to do further processing, i.e. full screen display
        }
    });
}

/**
 * Adapter for our image files.
 */
private class ImageAdapter extends BaseAdapter {

    private Context context;

    public ImageAdapter(Context localContext) {
        context = localContext;
    }

    public int getCount() {
        return cursor.getCount();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView picturesView;
        if (convertView == null) {
            picturesView = new ImageView(context);
            // Move cursor to current position
            cursor.moveToPosition(position);
            // Get the current value for the requested column
            int imageID = cursor.getInt(columnIndex);
            // Set the content of the image based on the provided URI
            picturesView.setImageURI(Uri.withAppendedPath(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
            picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            picturesView.setPadding(8, 8, 8, 8);
            picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
        }
        else {
            picturesView = (ImageView)convertView;
        }


           return picturesView;
        }
    }
}