2

我很好奇如何从 Android 中的 Gallery/Camera 文件夹中获取图像。我正在查看文件管理器,但无法真正了解这些图像在文件系统中的位置。如果我转到文件管理器,我无法找到拍摄照片的确切位置。如果我去图库应用程序,我会看到它们挂在“相机”文件夹中。

关于我想要完成的一些事情:

获取所有现有照片,在某个时候保存并将它们显示在我的活动中。我正在尝试允许电话用户从现有照片中为他们的个人资料分配头像。

4

2 回答 2

4

这是代码,以防有人需要。这是在姜饼上测试的。

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());
        }
于 2011-03-17T21:51:59.790 回答
1

这个答案可能会有所帮助。这是一个常见问题,因此如果您在 StackOverflow 中搜索Android camera ,您可能会得到结果。

于 2011-03-15T17:31:31.667 回答