android - get images from camera shot

dropsOfJupiter picture dropsOfJupiter · Mar 15, 2011 · Viewed 7.6k times · Source

I am curious as to how to get images from the Gallery/Camera folder in Android. I am looking into the file manager and can't really get a grasp as to where the those images are located in the file system. If I go to File Manager I can't locate the exact location of my taken photos. If I go to Gallery app I see them hanging in the "Camera" folder.

A little about what I am trying to accomplish:

Get all existing photos, saved at some point and display them on my activity. I am trying to allow the phone user to assign an avatar to their profile from existing photos.

Answer

dropsOfJupiter picture dropsOfJupiter · Mar 17, 2011

here is the code, in case somebody needs it. This is tested on gingerbread.

List<Image> existingPhotos = getCameraImages(this);

            photosGrid = (GridView)findViewById(R.id.gvExistingPhotos);
            LazyPhotosGridAdapter adapter = new LazyPhotosGridAdapter(this, existingPhotos);
            photosGrid.setAdapter(adapter);





public String getRealPathFromURI(Uri contentUri) {
            String[] proj = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }

        public static List<Image> getCameraImages(Context context) {
            final String[] projection = { MediaStore.Images.Media.DATA };
            final String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
            final String[] selectionArgs = { CAMERA_IMAGE_BUCKET_ID };
            final Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, 
                    projection, 
                    selection, 
                    selectionArgs, 
                    null);
            List<Image> result = new ArrayList<Image>(cursor.getCount());
            if (cursor.moveToFirst()) {
                final int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                do {
                    final String data = cursor.getString(dataColumn);
                    result.add(new Image(data));
                } while (cursor.moveToNext());
            }
            cursor.close();
            return result;
        }

        public static final String CAMERA_IMAGE_BUCKET_NAME =
            Environment.getExternalStorageDirectory().toString()
            + "/DCIM/Camera";
        public static final String CAMERA_IMAGE_BUCKET_ID =
                getBucketId(CAMERA_IMAGE_BUCKET_NAME);

        /**
         * Matches code in MediaProvider.computeBucketValues. Should be a common
         * function.
         */
        public static String getBucketId(String path) {
            return String.valueOf(path.toLowerCase().hashCode());
        }