打开Android图库,不在采摘模式。可能吗?

时间:2012-02-08 11:32:21

标签: android android-intent image-gallery

有没有办法打开图片库,但是没有选择模式?

我看到的每个例子都是选择模式,使用startActivityForResult

提前致谢!

1 个答案:

答案 0 :(得分:0)

使用GalleryActivity,main.xml,galleryitem.xml尝试以下代码:

    // Gallery Activity

    public class Gallery extends Activity {
        private int count;
        private Bitmap[] thumbnails;
        private ImageAdapter imageAdapter;
        String selectImages = "";
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.gallery);


                    final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };

                    final String orderBy = MediaStore.Images.Media._ID;

                    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];
                    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); //Log.i("selected path: ", arrPath[i]);
                    }
                    imagecursor.close();

            GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
            imageAdapter = new ImageAdapter();
            imagegrid.setAdapter(imageAdapter);


        }

        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            GeneralUtility.setContext(this);
        }

        public class ImageAdapter extends BaseAdapter 
        {
            private LayoutInflater mInflater;


            public ImageAdapter() {
                mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }

            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) 
            {
                ViewHolder holder;
                if (convertView == null) {
                    holder = new ViewHolder();
                    convertView = mInflater.inflate(
                            R.layout.galleryitem, null);
                    holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

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

                holder.imageview.setImageBitmap(thumbnails[position]);
                holder.imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
                holder.id = position;
                return convertView;
            }
        }
        class ViewHolder {
            ImageView imageview;
            int id;
        }

        }

    // main.xml file

    <?xml version="1.0" encoding="utf-8"?>
    <LeniarLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">


        <GridView
            android:id="@+id/PhoneImageGrid"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:numColumns="auto_fit"
            android:verticalSpacing="10dp"
            android:horizontalSpacing="10dp"
            android:columnWidth="90dp"
            android:stretchMode="columnWidth"
            android:gravity="center"
            android:layout_above="@id/ll"
            android:layout_below="@id/l2" />


    </LeniarLayout>

  // galleryitem.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView
        android:id="@+id/thumbImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>