0

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

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(),

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {

        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");

        imageView.setImageBitmap(bmp);

    }

}

如果有人向我提供如何使用从相机拍摄的图像动态更新图库的示例,我会很高兴。

4

2 回答 2

1

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

File sdDir = new File("/sdcard/Pictures/YOUR_DIR");
sdDirFiles = sdDir.listFiles();
for (i = 0; i < sdDirFiles.length; i++) {
//ADD to gallery HERE..
}
于 2012-05-07T04:42:43.777 回答
1
如果您在特定存储桶(文件夹)的 sdcard 下获取所有图像,则使用此代码...
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;
        }

    }
}
使用此代码,您可以从特定文件夹中获取所有图像,并在捆绑包中传递存储桶显示名称。如果您从 sdcard 中获取所有存储桶(文件夹)的所有图像,则使用此代码
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;
        }
    }
}
于 2012-05-07T05:46:04.327 回答